Home >Backend Development >Python Tutorial >Import study notes in python

Import study notes in python

高洛峰
高洛峰Original
2017-02-03 16:05:201579browse

Preface

There are two ways to organize modules in python. One is a simple python file, and the file name is the module name. The other is a package. A package is a directory containing several python files. There must be a file __init__.py in the directory, so the directory name is the module name. The python files in the package can also be imported in the form of package name.file name

import syntax

## There are two types of #import syntax

1. Directly import the module

import Module
import Module as xx

2. Import objects from the module (subordinate modules, classes, functions, variables, etc.)

from Module import Name
from Module immport Name as yy

as syntax is used to set the alias of an object (object here refers to modules, classes, functions, etc.), and import introduces the object name into the name space of the current file

Assume that there is the following directory structure

├── A.py
└── pkg
 ├── B.py
 └── __init__.py

In the current directory, the following statements are valid

import A
import pkg
import pkg.B
from pkg import B

In order to simplify the discussion, the following will not Give an example of as syntax

import steps


All loaded module information in python is stored in the sys.modules structure. When importing a module, the following steps will be performed

If it is import A, check whether there is already A in sys.modules. If there is, it will not be loaded. If not, create a module object for A and load A

If it is from A import B, First create a module object for A, then parse A, find B from it and fill it into A's __dict__


Nested import


We may You will worry about whether a module will be imported multiple times. Suppose there are three modules A, B, and C. A needs to import B and C, and B needs to import C. In this way, A will execute import C twice, once by itself. The import is executed once when importing B. However, according to the import steps mentioned above, it is found that the module has been loaded during the second import, so the import

will not be repeated, but an error will be reported in the following situations.

#filename: A.py
from B import BB
class AA:pass
 
#filename: B.py
from A import AA
class BB:pass

At this time, no matter whether A.py or B.py is executed, an ImportError exception will be thrown. Assuming that we are executing A.py, the reasons are as follows

When file A.py executes from B import BB, B.py will be scanned first, and a module object will be created for B in the namespace of A, trying to find BB from B

Scan the first line of B.py Execute from A import AA, and then scan A.py

Scan the first line of A.py and execute from B import BB. Since the module object has been created for B in step 1, it will be directly imported from B. Obtain BB from the __dict__ of the module object. At this time, it is obvious that BB cannot be obtained, so an exception is thrown.


There are two ways to solve this situation.

Change from B import BB is changed to import B, or from A import AA is changed to import A

Exchange the two lines of code in A.py or B.py


In short, import It should be noted that, try to import the

package when you need to use it. When there is an __init__.py file in a directory, the directory is a python package.


Importing a package is the same as importing a single file. We can make an analogy like this:

When importing a single file, the classes, functions, and variables in the file can all be used as import objects

When importing a package, the subpackages, files in the package, and the classes, functions, and variables in __init__.py can all be used as import objects.

Assume that there is the following directory structure

pkg
├── __init__.py
└── file.py

The content of __init__.py is as follows

argument = 0
class A:pass

It is OK to execute the following statements in the same directory as pkg

>>> import pkg
>>> import pkg.file
>>> from pkg import file
>>> from pkg import A
>>> from pkg import argument

But the following statement is wrong

>>> import pkg.A
>>> import pkg.argument

ImportError: No module named xxx is reported, because when we execute import A.B, both A and B must be modules (files or packages)

Relative import and absolute import

The format of absolute import is import A.B or from A import B, and the format of relative import is from . import B or from ..A import B, . represents The current module, ..represents the upper-level module,...represents the upper-level module, and so on. When we have multiple packages, there may be a need to import the contents of one package into another, which will result in an absolute import, and this is often when errors are most likely to occur. Let’s illustrate with a specific example


The directory structure is as follows

app
├── __inti__.py
├── mod1
│ ├── file1.py
│ └── __init__.py
├── mod2
│ ├── file2.py
│ └── __init__.py
└── start.py

The content of app/start.py is import mod1.file1

The content of app/mod1/file1.py is from ..mod2 import file2


In order to facilitate analysis, we add print __file__, __name__

to the first line of all py files (including __init__.py) now in app/mod1/file1.py Relative import is used. When we execute python file1.py under app/mod1 or python mod1/file1.py under app, an error will be reported ValueError: Attempted relative import in non-package

Execute python under app - m mod1.file1 or python start.py will report an error ValueError: Attempted relative import beyond toplevel package

The specific reasons will be discussed later, let’s first take a look at some rules when importing modules

If it is not clear When specifying the package structure, python determines the structure of a module in the package based on __name__. If it is __main__, it is the top-level module itself and has no package structure. If it is the A.B.C structure, then the top-level module is A. .

Basically follow this principle

If it is an absolute import, a module can only import its own submodules or modules and submodules at the same level as its top-level module

If it is a relative import, a module must have a package structure and can only import modules inside its top-level module

The directory structure is as follows

A
├── B1
│ ├── C1
│ │ └── file.py
│ └── C2
└── B2

其中A,B1,B2,C1,C2都为包,这里为了展示简单没有列出__init__.py文件,当file.py的包结构为A.B1.C1.file(注意,是根据__name__来的,而不是磁盘的目录结构,在不同目录下执行file.py时对应的包目录结构都是不一样的)时,在file.py中可采用如下的绝对的导入

import A.B1.C2
import A.B2

和如下的相对导入

from .. import C2
from ... import B2

什么情况下会让file.py的包结构为A.B1.C1.file呢,有如下两种

在A的上层目录执行python -m A.B1.C1.file, 此时明确指定了包结构

在A的上层目录建立文件start.py,在start.py里有import A.B1.C1.file,然后执行python start.py,此时包结构是根据file.py的__name__变量来的

再看前面出错的两种情况,第一种执行python file1.py和python mod1/file1.py,此时file.py的__name__为__main__ ,也就是说它本身就是顶层模块,并没有包结构,所以会报错

第二种情况,在执行python -m mod1.file1和python start.py时,前者明确告诉解释器mod1是顶层模块,后者需要导入file1,而file1.py的__name__为mod1.file1,顶层模块为也mod1,所以在file1.py中执行from ..mod2 import file2时会报错 ,因为mod2并不在顶层模块mod1内部。通过错误堆栈可以看出,并不是在start.py中绝对导入时报错,而是在file1.py中相对导入报的错

那么如何才能偶正确执行呢,有两种方法,一种是在app上层目录执行python -m app.mod1.file1,另一种是改变目录结构,将所有包放在一个大包中,如下

   
app
├── pkg
│ ├── __init__.py
│ ├── mod1
│ │ ├── __init__.py
│ │ └── file1.py
│ └── mod2
│ ├── __init__.py
│ └── file2.py
└── start.py

start.py内容改成import pkg.mod1.file1,然后在app下执行python start.py

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如有疑问大家可以留言交流。

更多python中import学习备忘笔记相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn