If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.html
若是你關閉Python解釋器(IDLE),而後再打開,那麼之前變量的定義則會丟失。所以,若是你想要寫長一點的程序,最好先在編輯器中編寫Python程序,而後將文件做爲輸入導入到IDLE解釋執行。這就是所謂的建立一個腳本。當你的程序變得愈來愈大,你可能想要將程序分開到幾個文件中。你可能想要寫一個便利的函數,可以在幾個程序中重複利用。python
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).程序員
爲了支持這種功能,Python能夠把代碼用文件存儲,並做爲一個腳本,或用來與解釋器交互。這種文件就叫作模塊。模塊中定義能夠被導出到另外一個模塊中或導入到main模塊中。shell
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:express
一個模塊就是一個文件,這個模塊包含了Python的定義和語句。文件的名字就是模塊的名字加後綴.py。在模塊內部,該模塊的名字可使用全局變量__name來表示。例如,使用你最喜歡的文本編輯器建立一個文件fibo.py,文件內容以下:api
# Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result
Now enter the Python interpreter and import this module with the following command:緩存
打開Python IDLE,用下面的命令導入模塊(通常Python是經過sys.path指定的路徑搜索模塊):session
>>> import fibo
This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions:架構
這個導入命令,不會將該模塊的全部函數的名字本身導入到當前的符號表中,它僅僅只是將模塊的名字導入到當前的符號表中,使用模塊的名字,你能夠訪問它的函數:app
>>> fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo'
If you intend to use a function often you can assign it to a local name:
你也能夠將函數賦給本地變量:
>>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. [1] (They are also run if the file is executed as a script.)
模塊包含可執行語句和函數定義。這些語句用於初始化模塊,他們僅僅在模塊第一次導入時被執行。
Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, modname.itemname.
每個模塊有它本身的符號表,這個符號表能夠看成是存儲模塊中全部函數定義的全局符號表。所以,模塊的擁有者能夠在模塊中使用全局變量而不用擔憂和另外一個用戶的全局變量引起衝突。另外一方面,若是你很清楚你的意圖,你能夠訪問模塊的函數,請使用modname.itemname
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.
模塊可以導入其餘模塊。一種約定俗成的作法是:將全部import語句放在模塊的開頭。被導入的模塊名字會被放置在當前模塊的全局符號表中。
There is a variant of the import statement that imports names from a module directly into the importing module’s symbol table. For example:
這裏列舉了幾種不一樣形式的import語句,從一個模塊中直接導入函數到當前模塊的符號表中:
>>> from fibo import fib, fib2 >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, fibo is not defined).
這種導入方式不會將模塊名字fibo放入到當前模塊的符號表中(例如,輸入fibo是未定義的)。
There is even a variant to import all names that a module defines:
這是另外一種導入形式:導入全部在模塊中定義的名字:
>>> from fibo import * >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.
這種導入方式會將全部的名字導入到當前模塊的符號表中,除開那些如下劃線_開頭的變量。在大部分狀況下,Python程序員是不會使用這種導入方式的。由於這樣會使你導入一些你未知的一些名字到解釋器中,可能就會覆蓋已經定義的變量。
Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
注意,通常使用import * 是不被同意的,由於它的可讀性不好。然而,在解釋器中爲了節省輸入,這種形式的導入也是OK的。僅此而已。
When you run a Python module with
當你這樣運行Python模塊:
python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:
當你輸入上面的語句時,fibo模塊將會被執行,前提是__name__必須設爲"__main__"。這就意味着你必須在fibo模塊最後面加入下面的代碼:
if __name__ == "__main__": import sys fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the 「main」 file:
你不但能夠把它當腳本使用,還能夠稱爲可導入的模塊,由於分析命令行的代碼僅僅當模塊的文件名爲main.py時纔會被執行。
If the module is imported, the code is not run:
若是導入模塊,代碼不會運行:
>>> import fibo >>>
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
當模塊spam被導入後,解釋器首先在內置的模塊中尋找該模塊的名字。若是沒找到,就會在變量sys.path指定的路徑中尋找文件名爲spam.py的文件。sys.path初始化的路徑包括:
After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
當sys.path初始化後,Python程序能夠修改sys.path的路徑。當前正在運行的腳本所在的目錄放置在搜索路徑的最開頭,也就是標準庫模塊路徑的前面。這就意味着若是當前模塊的名字和標準庫模塊的名字相同時,則會替換掉該標準庫模塊。若是你不是有意要替換該標準庫模塊,那麼可能就會出現錯誤。
To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc. This naming convention allows compiled modules from different releases and different versions of Python to coexist.
爲了加速加載模塊。Python緩存了已經編譯好的每一個模塊的版本,並放置在__pycache__目錄下。編譯好的模塊的名字爲:module.version.pyc(module爲對應的模塊名,version爲Python對應的版本)。例如:在CPython release 3.3版本上編譯spam.py將會緩存__pycache__/spam.cpython-33.pyc。這種緩存的命名風格容許不一樣的Python版本共存。
Python checks the modification date of the source against the compiled version to see if it’s out of date and needs to be recompiled. This is a completely automatic process. Also, the compiled modules are platform-independent, so the same library can be shared among systems with different architectures.
針對編譯的版本,Python檢查源碼的修改時間判斷該版本是否過時或者須要從新編譯。這徹底是一個自動完成的過程。一樣,編譯的模塊也是平臺獨立的,所以一樣的庫能夠在不一樣架構的系統上運行。
Python does not check the cache in two circumstances. First, it always recompiles and does not store the result for the module that’s loaded directly from the command line. Second, it does not check the cache if there is no source module. To support a non-source (compiled only) distribution, the compiled module must be in the source directory, and there must not be a source module.
Python在兩種狀況下不會檢查緩存。第一,若是模塊執行從命令行加載,則該模塊會被從新編譯,而且不會保存結果。第二,若是沒有源碼的模塊,將不會檢查緩存。爲了支持只有編譯版本的模塊,必須將該模塊放置在源碼模塊目錄下,而且不能有該模塊的源碼。
Some tips for experts:
Python comes with a library of standard modules, described in a separate document, the Python Library Reference (「Library Reference」 hereafter). Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls. The set of such modules is a configuration option which also depends on the underlying platform. For example, the winreg module is only provided on Windows systems. One particular module deserves some attention: sys, which is built into every Python interpreter. The variables sys.ps1 and sys.ps2 define the strings used as primary and secondary prompts:
Python有一個標準模塊庫,該庫的說明在另外一個單獨的文檔中,叫作Python庫參考。有些模塊是解釋器內置的(意味着不須要使用import語句);雖然這些內置的模塊提供了操做權限(例如訪問系統調用),但卻不是語言核心的一部分。這些模塊是能夠經過配置來選擇的,而且依賴於不一樣的平臺。例如:winreg模塊僅僅在Windows系統中使用。另外一個特殊的模塊sys值得引發注意,它內置於每個Python解釋器中。變量sys.ps1和sys.ps2定義了兩個提示符字符串:
>>> import sys >>> sys.ps1 '>>> ' >>> sys.ps2 '... ' >>> sys.ps1 = 'C> ' C> print('Yuck!') Yuck! C>
These two variables are only defined if the interpreter is in interactive mode.
這兩個變量僅僅是在解釋器在交互模式的環境中才被定義。
The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:
變量sys.path包含一個字符串的列表,它以爲了解釋器尋找模塊的搜索路徑。環境變量PYTHONPATH的路徑做爲它初始化的一部分,若是沒有設置PYTHONPATH環境變量,則使用默認的內置路徑。你能夠標準的列表操做來修改它:
>>> import sys >>> sys.path.append('/ufs/guido/lib/python')
The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings:
內置的函數dir()常被用來尋找那些已經被模塊定義了的變量名字,它返回一個已經排序好了的字符串:
>>> import fibo, sys >>> dir(fibo) ['__name__', 'fib', 'fib2'] >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
Without arguments, dir() lists the names you have defined currently:
若是不輸入參數,dir()函數將會列出當前已經定義的名字:
>>> a = [1, 2, 3, 4, 5] >>> import fibo >>> fib = fibo.fib >>> dir() ['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
Note that it lists all types of names: variables, modules, functions, etc.
注意:它會列出全部的名字:變量,模塊,函數等等。
dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module builtins:
dir()不會雷錘內置的函數名和變量名。若是你想要列出這些名字,他們被定義在標準模塊builtins中:
>>> import builtins >>> dir(builtins) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
Packages are a way of structuring Python’s module namespace by using 「dotted module names」. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.
包是一種構建Python模塊命名空間的方式,經過使用"packagename.modulename"。例如,模塊名:A.B 說明子模塊B在包A中。
Suppose you want to design a collection of modules (a 「package」) for the uniform handling of sound files and sound data. There are many different sound file formats (usually recognized by their extension, for example: .wav, .aiff, .au), so you may need to create and maintain a growing collection of modules for the conversion between the various file formats. There are also many different operations you might want to perform on sound data (such as mixing, adding echo, applying an equalizer function, creating an artificial stereo effect), so in addition you will be writing a never-ending stream of modules to perform these operations. Here’s a possible structure for your package (expressed in terms of a hierarchical filesystem):
假設你但願設計一個模塊的集合(也就是設計一個包)來統一處理音頻文件和音頻數據。這裏有許多不一樣的音頻格式(一般能夠經過後綴來識別,例如.wav,.aiff,.au),所以你可能須要建立和維護增長的模塊來處理不一樣的文件格式之間的轉換。一樣,你可能也想要在處理音頻數據上執行多種操做,所以,你將會寫一個無盡的流模塊來處理這些操做。這裏有一個包的設計架構多是你須要的:
sound/ Top-level package __init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ...
When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.
當你導入包的時候,Python將經過sys.path指定的路徑來尋找包的子目錄。
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
__init__.py文件是必需要存在的,這樣Python纔會將目錄看做是包; 若是你使用一個廣泛的名字命名包,例如string,若是後面有string模塊,則會使該模塊失效。最簡單的狀況是,__init__.py是一個空文件,可是它也能夠包含包的初始化代碼或者設置變量__all__的值,下面將會更詳細的表述。
Users of the package can import individual modules from the package, for example:
包的使用者,能夠從包中導入單個模塊,例如:
import sound.effects.echo
This loads the submodule sound.effects.echo. It must be referenced with its full name.
加載子模塊sound.effects.echo.調用時必須使用全名稱:
sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
An alternative way of importing the submodule is:
另外一種替代方式是:
from sound.effects import echo
This also loads the submodule echo, and makes it available without its package prefix, so it can be used as follows:
這也是加載子模塊echo,調用時不須要加包的前綴,所以能夠這樣使用:
echo.echofilter(input, output, delay=0.7, atten=4)
Yet another variation is to import the desired function or variable directly:
還有一種方式是直接導入想調用的函數:
from sound.effects.echo import echofilter
Again, this loads the submodule echo, but this makes its function echofilter() directly available:
一樣,加載子模塊echo,可是能夠直接使用函數echofilter():
echofilter(input, output, delay=0.7, atten=4)
Note that when using from package import item, the item can be either a submodule (or subpackage) of the package, or some other name defined in the package, like a function, class or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised.
注意,當使用語句from package import item,這個item能夠是子模塊(或子包)也能夠是包裏面定義的對象,好比函數,類或者變量。import語句首先檢查item是否在包中已經定義,若是沒有,它就認爲這是一個模塊,並嘗試加載它,若是加載失敗,則拋出ImportError異常。
Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.
相反,當使用語句import item.subitem.subsubitem時,除開最後一個subsubitem,其餘都必須是包;最後一個subsubitem能夠是模塊,也能夠是包,但不能是類或者函數、變量。
Now what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.
當用戶使用from sound.effects import*時,則全部的模塊都被導入,這將會花費很長的時間,而且可能產生意想不到的負面效果。
The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package. For example, the file sound/effects/__init__.py could contain the following code:
惟一的解決辦法就是包的做者提供一個顯式的包的索引。import語句使用下面的慣例:若是一個包的__init__.py文件定義了變量__all__時,當使用語句from package import*,則會導入__all__變量所定義列表中的值。當有新版本的包要發佈時,應該由做者保持對該列表的更新。看成者不想看到用戶使用import *語句時,包的做者也能夠決定不使用它。例如,文件sound/effects/__init__.py包含下面的代碼:
__all__ = ["echo", "surround", "reverse"]
This would mean that from sound.effects import * would import the three named submodules of the sound package.
這就意味着from sound.effects import *將會導入effects包的三個子模塊。
If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements. Consider this code:
若是沒有定義變量__all__,語句from sound.effects import*將不會把effect包的子模塊導入到當前的命名空間中;它僅僅只確保包sound.effects被導入,而後再導入在effects包中定義的變量名。這包含任何在__init__.py文件中定義的變量名。考慮下面的代碼:
import sound.effects.echo import sound.effects.surround from sound.effects import *
In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from...import statement is executed. (This also works when __all__ is defined.)
在這個例子中,模塊echo和surround被導入到當前的命名空間中,由於當from...import語句被執行時,他們就被定義在sound.effects包裏面。
Although certain modules are designed to export only names that follow certain patterns when you use import *, it is still considered bad practise in production code.
儘管某些模塊只是用來導出變量名,但import *始終被認爲是一種很差的導入方式。
Remember, there is nothing wrong with using from Package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.
記住,使用from Package import specific_submodule是不會錯的!實際上,這也是被建議的一種方式,
When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.
當一個包有子包時(例如包sound),你可使用絕對路徑引用包同級的子模塊。例如,若是模塊sound.filters.vecoder須要使用sound.effects包中echo模塊,可使用語句:from sound.effects import echo。
You can also write relative imports, with the from module import name form of import statement. These imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module for example, you might use:
你也可以用相對路徑導入經過 rom module import name形式的導入語句。這些導入語句須要使用 . 來表示當前或父包。例如當在surround模塊中使用下面的其餘子模塊時,你可使用:
from . import echo from .. import formats from ..filters import equalizer
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.
注意,相對路徑的導入是基於當前模塊的。由於main模塊的名字老是"__main__",若是模塊打算看成Python應用程序的主模塊使用時,必須老是使用絕對路徑。
Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.
包還支持其餘特殊的屬性,例如__path__。這個變量定義了一個列表,該列表包含了包的__init.__py所在的目錄路徑。這個變量能被修改。這將影響包中對子包和子模塊的搜索。
While this feature is not often needed, it can be used to extend the set of modules found in a package.
然而這種特性不是常須要的,它可以用來擴展包中子模塊的搜索路徑。