1. Module
In Python, a file can be viewed as a standalone module, and the package corresponds to a folder, and the module divides the Python code into organized pieces of code to implement code reuse in a way that is imported.
1.1 Module Search Path
When importing a module, the module is searched according to the value of the Sys.path variable, and the value of Sys.path is a list of each independent path containing the current directory, the Python installation directory, the PYTHONPATH environment variable, The search order follows the order of the Paths in the list (typically the current directory has the highest precedence).
1>>>Import SYS2>>>Sys.path3["','E:\\python27\\lib\\idlelib','C:\\windows\\system32\\python27.zip','E:\\python27\\dlls','E:\\python27\\lib','E:\\python27\\lib\\plat-win','E:\\PYTHON27\\LIB\\LIB-TK','e:\\python27','e:\\python27\\lib\\site-packages']
1.2 Importing Modules
1.2.1 Importing a module using the import statement
There are two different ways
Import Module1import module2import Module3import module1,module2,module3
The effect of the two methods is the same, but the first readability is better than the second one, it is recommended to import the module in the following order, and generally import all the modules in the file header
- Python Standard library
- Third-party modules
- Application Custom Modules
You can also import modules inside a function so that the imported module scope is local
1.2.2 Importing a module's properties using the From-import statement
Single-line Import
1 from module import Name1,name2,name3
Multi-line Import
From module import name1,name2, Name3
Import all properties (because it is easy to overwrite the existing name in the current namespace, so it is generally deprecated, suitable for cases where variable names are very long in the module and many variables)
1 from module import *
If you do not want the properties of a module to be imported by the above method, you can add an underscore (_test) to the property name, and if you need to unhide it, you can display the import of the property (from module import _test)
Import statements for 1.2.3 Extensions
Replace the original name of the module with a custom name
as JSON
When the module is imported, the top-level code of the module is executed when it is loaded, such as setting global variables, declaring classes and functions, etc., so the code should be encapsulated in classes and functions as much as possible. a module, no matter how many times it is imported, is loaded only once, preventing the code from being executed multiple times when the import occurs.
1.2.4 re-importing the module reroad
1) Import
Import function: Imports/introduces a Python standard module that includes a. py file, a directory with a __init__.py file.
Import Module_name[,module1,...] From module import *|child[,child1,...]
Note: When the import statement is reused multiple times, the specified module is not reloaded, only the memory address of the module is referenced to the local variable environment.
a.py #!/usr/bin/env python #encoding: utf-8 import os print ' In a ', ID (OS) m.py #!/usr/bin/ Env python #encoding: utf-8 Import a #第一次会打印a里面的语句 the import os #再次导入os后, whose memory address is the same as the one inside, So here is just a local reference to the OS print ' in C ', ID (OS) import a #第二次不会打印a里面的语句, because there is no reload
#结果
In a 23124144
In C 23124144
2) Reroad
Function: The module has been loaded to reload, generally used for the original module has changed and other special cases, reload before the module must have been import.
Import osreload (OS)
Note: The reload will reload the loaded module, but the old one will still be used, and the new production instance will use the new module;
Reload or use the original memory address;
Cannot support from: Import: The module in the format is reloaded.
1 a.py2#!/usr/bin/env python3#encoding: utf-8 4 Import OS5Print'In a', ID (OS)6 7 m.py8#!/usr/bin/env python9#encoding: utf-8 Ten Import a #第一次import会打印a里面的语句 One Print ID (a) #原来a的内存地址 A Reload (a) #第二次reload还会打印a里面的语句 because there is a reload -Print ID (a) #reload后a的内存地址, same as the original
#结果
>>>
In a 23058608
29617680
In a 23058608
29617680
>>>
2. Package
A package is another module that can contain other modules;
When the module is stored in a file (extension. py), the package is the directory where the module resides;
Package must include a __init__.py file (module), if it is a normal module import file content is the contents of the package
#名为constants的包, the file constants/__init__.py includes the statement Pi=3.14import Constantsprint constants. Pi
In order to place the module in the package, simply place the module in the package directory:
3. Specify how Python files are encoded
Python, by default, uses ASCII encoding, which allows you to specify the encoding method, such as
12 |
#!/usr/bin/env python #coding=utf-8 |
Or
12 |
#!/usr/bin/env python # -*- coding:utf-8 -*- |
4. Resolve Import Cycle Issues
There are two modules below, a.py and b.py
a.py
1234567 |
#!/usr/bin/env python #coding=utf-8 import b if __name__ = = ‘__main‘ : print ‘hello,I‘ m a‘ |
1234567 |
#!/usr/bin/env python #coding=utf-8 import a if __name__ = = ‘__main‘ : print ‘hello,I‘ m b‘ |
Here a attempts to import B, and B also attempts to import a, importing a module that was not previously fully imported, causing the import to fail. Workaround: Remove an import statement, put the import statement inside the function, and import it when needed.
b.py
123456 |
#!/usr/bin/env python #coding=utf-8 if __name__ = = ‘__main‘ : import a print ‘hello,I‘ m b‘ |
5. Tips for use
Create a directory Python_apps, edit ~/.BASHRC, set the value of the environment variable Pythonpath to the path of the directory.
1 |
export PYTHONPATH= /home/zhoujh/python_apps :$PYTHONPATH |
Then in the Python_apps directory to do a soft link to the application directory, if you want to run the application path is:/home/zhoujh/python_workspace/app
1 |
ln -s /home/zhoujh/python_workspace/app . /app |
To add a new app later, just create a link in that directory.
Python Basic Tutorial Summary 9--module, package, standard library