When we create a module of our own, the module itself comes with several global variables, which are present in each file.
View py
Global variables for the current file
[[email protected] ~]# echo ' Print (VARs ()) ' > scripts.py [[email protected] ~]# python scripts.py {' __builtins__ ': <module ' __builtin__ ' (built-in), ' __name__ ': ' __main__ ', ' __file__ ': ' scripts.py ', ' __doc__ ': None, ' __package_ _ ': None}
Default global variables
Variable name |
Description |
__doc__ |
Note for the py file, which is a comment on the file |
__file__
Gets the current file path |
__package__
Returns the package of the imported file, using . Delimited, if current file returns none |
__cached__
Return additional files imported PYC file path, current file returns none |
_name_ |
If it is the primary file, return __main__ otherwise equals the module name |
What's not listed is no need to know.
Instance
return scripts
notes and file paths for files
[[email protected] ~]# cat scripts.py #!/usr/bin/env pyton# file Comment Shelf write "" "Python File Comments" "" # Output File Comment print ("comments", __doc__) # Output file path print ("File path", __file__)
Execute script
[[email protected] ~]# python scripts.py (' comments ', ' Python file Comments ') (' File path ', ' scripts.py ')
__name__
Application of the function
View files and file contents under the current directory
[Email protected] ~]# Ls-ltotal 8drwxr-xr-x 2 root root 4096 may 21:36 lib-rw-r--r--1 root root and May 21:37 SC Ripts.py[[email protected] ~]# ls-l lib/total 12-rwxr-xr-x 1 root root Wu may 21:36 f1.py-rw-r--r--1 root root 0 M Ay 21:34 __init__.py[[email protected] ~]# cat lib/f1.py#!/usr/bin/env pythondef Echo (): Print ("lib/f1.py") [[Email Pro Tected] ~]# cat scripts.py #!/usr/bin/env pytonfrom lib import f1if __name__ = "__main__": F1.echo ()
Execute ' scripts.py ' script, the script will call ' lib/f1.py ' file, and ' lib/f1.py ' file will output ' lib/f1.py ', so the ' scripts.py ' file will naturally output ' lib/f1.py ' ' bash [[email protected] ~]# Python scripts.py # As we've seen, it will output the following content lib/f1.py
This is where we create a scripts1.py
file again
[email protected] ~]# Touch scripts1.py
scripts1.py
The file contents are as follows
[email protected] ~]# cat scripts1.py #!/usr/bin/env pytonimport scripts
File created well after executing him we'll find nothing output
[email protected] ~]# python scripts1.py
Then we change the contents of the scripts.py
script file to the following
[[email protected] ~]# cat scripts.py#!/usr/bin/env pytonfrom lib import f1f1.echo ()
scripts1.py Script This is going to output lib/f1.py
[email protected] ~]# python scripts1.py lib/f1.py
Why does the above situation occur?
Because the originalscripts.py
The file adds the If judgment, which means that if__name__
The value is equal to"__main__"
, then I'll execute the F1.echo () method, or else do nothing, whenscripts.py
When the file was directly executed by us.__name__
return result is"__main__"
, if it is called by another file, then__name__
The returned result is"scripts"
。
Automatically add the directory below the current directory bin
to the Python PATH
variable
Directory structure and script contents are as follows
[Email protected] ~]# tree/tmp//tmp/├──bin│├──f1.py│└──__init__.py└──scripts.py1 directory, 3 Files[[email Pro Tected] ~]# cat/tmp/scripts.py #!/usr/bin/env pytonimport osimport syspath = Os.path.dirname (__file__) Mod_path = Os.path . Join (Path, "Bin") Sys.path.append (Mod_path) for n in Sys.path:print (n)
Execution results
[Email protected] ~]# python/tmp/scripts.py/tmp/usr/lib64/python26.zip/usr/lib64/python2.6/usr/lib64/python2.6/ plat-linux2/usr/lib64/python2.6/lib-tk/usr/lib64/python2.6/lib-old/usr/lib64/python2.6/lib-dynload/usr/lib64/ python2.6/site-packages/usr/lib64/python2.6/site-packages/gtk-2.0/usr/lib/python2.6/site-packages/usr/lib/ Python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info/tmp/bin
This site is open source. Improve this page.
Module Default global variables for the Python standard library series