Модули в Python - это файлы. Из модулей можно импортировать классы. Импорт сначала осуществляется из текущего модуля, а потом из подпапок. Если ничего не найдено, то из путей sys.path
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.
Первоисточники и ссылки¶
In []:
Начинать лучше со страницы документации
<br/>[Modules](https://docs.python.org/2/tutorial/modules.html)
<br/>[]()
<br/>[]()
<br/>[]()
<br/>[]()
<br/>[]()
<br/>[]()
<br/>[]()
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:
In [4]:
sys.path
Out[4]:
6.1.2. The Module Search Path¶
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:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
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.
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.
6.2. Standard Modules¶
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:
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:
In [5]:
# The variables sys.ps1 and sys.ps2 define the strings used as primary and secondary prompts:
sys.ps1,sys.ps2
Out[5]:
The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings¶
In [10]:
# Надо чаще просматривать, что есть в модулях
# Я совсем забыл про эту вохможность
dir(sys)
Out[10]:
In [11]:
# Without arguments, dir() lists the names you have defined currently
# Note that it lists all types of names: variables, modules, functions, etc.
dir()
Out[11]:
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 __builtin__:
In [12]:
import __builtin__
>>> dir(__builtin__)
Out[12]:
In [7]:
# Это совсем другая функция
!dir
6.4. Packages¶
Packages are a way of structuring Python’s module namespace by using “dotted module names”. When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.
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.
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.
6.4.1. Importing * From a Package¶
In [13]:
__path__
In []:
Посты чуть ниже также могут вас заинтересовать
Комментариев нет:
Отправить комментарий