search
HomeBackend DevelopmentPython TutorialImport study notes in python

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
How are arrays used in scientific computing with Python?How are arrays used in scientific computing with Python?Apr 25, 2025 am 12:28 AM

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

How do you handle different Python versions on the same system?How do you handle different Python versions on the same system?Apr 25, 2025 am 12:24 AM

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

What are some advantages of using NumPy arrays over standard Python arrays?What are some advantages of using NumPy arrays over standard Python arrays?Apr 25, 2025 am 12:21 AM

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

How does the homogenous nature of arrays affect performance?How does the homogenous nature of arrays affect performance?Apr 25, 2025 am 12:13 AM

The impact of homogeneity of arrays on performance is dual: 1) Homogeneity allows the compiler to optimize memory access and improve performance; 2) but limits type diversity, which may lead to inefficiency. In short, choosing the right data structure is crucial.

What are some best practices for writing executable Python scripts?What are some best practices for writing executable Python scripts?Apr 25, 2025 am 12:11 AM

TocraftexecutablePythonscripts,followthesebestpractices:1)Addashebangline(#!/usr/bin/envpython3)tomakethescriptexecutable.2)Setpermissionswithchmod xyour_script.py.3)Organizewithacleardocstringanduseifname=="__main__":formainfunctionality.4

How do NumPy arrays differ from the arrays created using the array module?How do NumPy arrays differ from the arrays created using the array module?Apr 24, 2025 pm 03:53 PM

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

How does the use of NumPy arrays compare to using the array module arrays in Python?How does the use of NumPy arrays compare to using the array module arrays in Python?Apr 24, 2025 pm 03:49 PM

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

How does the ctypes module relate to arrays in Python?How does the ctypes module relate to arrays in Python?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor