python module package notes

Module contains common subject matter associated with a set of functions, methods or data

Import available modules: the first module is mounted on the operating system

1 module start using the easiest way is to use the import keyword import it: import sys import sys name of the module that contains the main system and various related services provided by python.

Example: To create foods modules, only need to create foods.py file. Once created, by using the module name of the imported foods created without adding .py module name in the tail

python automatically searches the directory path or path list stored in the variable sys module. import sys sys.path

The method of increasing the append a directory, or add any number of catalogs extend method.

>>> import sys
>>> sys.path
['', 'D:\\Program Files\\Python\\Python37\\Lib\\idlelib', 'D:\\Program Files\\Python\\Python37\\python37.zip', 'D:\\Program Files\\Python\\Python37\\DLLs', 'D:\\Program Files\\Python\\Python37\\lib', 'D:\\Program Files\\Python\\Python37', 'D:\\Program Files\\Python\\Python37\\lib\\site-packages']
>>> sys.path.append("E:\pythonscript")
>>> sys.path
['', 'D:\\Program Files\\Python\\Python37\\Lib\\idlelib', 'D:\\Program Files\\Python\\Python37\\python37.zip', 'D:\\Program Files\\Python\\Python37\\DLLs', 'D:\\Program Files\\Python\\Python37\\lib', 'D:\\Program Files\\Python\\Python37', 'D:\\Program Files\\Python\\Python37\\lib\\site-packages', 'E:\\pythonscript']
>>> import foods
>>> dir(foods)
['Fridge', 'Omelet', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

When using import import module, which contains all names have been placed in the module named scope. In other words, the scope name is the name used in the import statement.

Using from 2 to import command modifier may be used directly from foods import Omelet name after import

from foods import Recipe

r=Recipe()

If there are multiple levels to be used in the name of the current scope. Need from foods.recipes import b

3 is sometimes necessary to do all of the content available in each module explicitly specify the name of the module is used. To this end it provides a python special characters *, which is used to from ... inport * statement. When imported into the global scope can only be used to import *

 

Package, similar to the directory folder structure. Use as a separate module files in the same directory must be used together .

Create a kitchen catalog and create __init__.py file in which the two underscores before and after each name. The document suggesting this is a python package directory, and not just an ordinary directory that contains the file python.

__init__ file contains the control packet usage code. When importing packages, each file in the directory does not import immediately. __init__.py file is first calculated, which specifies the file to use as well as ways to use them.

Add two classes in the kitchen folder: fridge.py omelet.py

1 is introduced in the class file __init__: after the introduction of these classes have become available kitchen package

from Fridge import Fridge
from Omelet import Omelet

The kitchen module needs to be loaded automatically added to the directory, such as where to find the fridge or prompt an error

>>> sys.path.append("E:\\pythonscript\\kitchen") 
>>> import kitchen
>>> f=kitchen.Fridge()

 

2 If you do not want to use kithche. __All__ need to be added in the list __init__.py file

from Fridge import Fridge
from Omelet import Omelet
__all__=['Fridge','Omelet']

>>> from kitchen import *
>>> f=Fridge()

3 If you do not use added from Omelet import Omelet __init__.py file need to reference: Omelet.Omelet ()

All __all__ name appears in the list of these names can only be exported by *.

__all__ elements appear in the list may be a function, or the name of the class data, * which can be automatically imported into the global scope of the import procedure

 

pyc file: This is a special python file output, which includes some form of code that is easier than plain text source code to quickly load and run. Such as where to .py file has been modified, by double-clicking it or running python -i or run menu, python will be re-created according to the updated source code .pyc file next time to call it.

The name of the source module and python modules are hidden under a special sys.modules, it contains all the modules already imported.

View loaded modules: list (sys.modules.keys ())

After loading the module, it will appear sys.modules the dictionary, then, even if you change the module, python is no longer recalculate it. Module may simply be removed from the post sys.modules dictionary, and then introduced into it again.

>>> import kitchen
>>> 'kitchen' in sys.modules
True
>>> sys.modules['kitchen']
<module 'kitchen' from 'E:\\pythonscript\\kitchen\\__init__.py'>
>>> sys.modules.pop('kitchen')
<module 'kitchen' from 'E:\\pythonscript\\kitchen\\__init__.py'>
>>> sys.modules['kitchen']
Traceback (most recent call l

python provides a built-in function reload, reloading it specified module, the same effects as the manual reload:

import kitchen

>>> import imp
>>> imp.reload(kitchen)

Note that this does not affect objects that already exist, you have to call such an object and re-create them

o=Omelet.Omelet()

 __name__ returns the name of the scope of the running, __ main__ is a special reserved word, which is the top global scope name.

Verify that the module is working properly, you can use the module at the end of this sentence: if __name __ == '__ main__'

 

python with a module to retain a local name for the scope. The internal name of the module may be directly used. To name external access module within a particular module, you first need to specify the module name, then the name followed by a period, and then later want to access.

In order to use a module, it must import statements into the program. Check the python sys.path list each directory until you find the file.

Generally desirable without having to enter the full path, using a specific part of a module. The full path name of the module to start, followed by the name of any intermediate module separated by a period, and finally the name of the actual need. It can be used to import import from ... name frequently used.

When a large amount of codes to be prepared, the package can be used to organize the code name, a directory structure after the start packet, the packet will be introduced into the program, the name of the directory is the package. __init__.py file directory will become an ordinary package. This file contains the code for the entire package useful, for example, all portions of the shared packet data, version information, and the storage location of important documents, etc., file also contains the necessary import statements, when the statement from ... import * with a request packet when the package files are not automatically exported, even if those files have been imported in __init__.py, you should specify the default name for the package need to export the list __all__ in.

 

Guess you like

Origin www.cnblogs.com/caojuansh/p/11579812.html