This article was first published on Zhihu

When using Python, library calls are often involved, which requires a basic knowledge of modules. This paper is divided into the following parts

  • The concept that
  • A simple call to the module
  • The package import
  • special__init__.pyfile
  • Search path for imported modules
  • __all__
  • Absolute versus relative references
  • Import Runtime nature
  • if __name__ == '__main__'

The concept that

This is to clarify the conceptual differences between modules, libraries, and packages in Python

  • A module is a PY file that defines functions, classes, variables, etc
  • A package is a folder formed by an aggregation of modules, which can contain multiple PY files or nested folders
  • A library is a reference to other programming languages and is a collection of code that performs a function, in the form of modules and packages in Python

A simple call to the module

For example, we have a tryModule folder with a first.py file that contains the following contents

a = 1

def myfun(s):

print(s + 1)

Copy the code

Open a command line window in the tryModule folder (hold down Shift, right-click, and select “Open command window here”) and type Python to enter command line interaction mode

>>> import first

>>> a

Traceback (most recent call last):

File "<stdin>", line 1.in <module>

NameError: name 'a' is not defined

>>> first.a

1

>>> first.myfun(2)

3

Copy the code

So the first.py file is a module that you can import with the first variable. Prefix, if you don’t want to use this prefix

>>> from first import a

>>> a

1

Copy the code

Other uses are as follows

# rename

>>> from first import myfun as addone

>>> addone(4)

5

Import all variables in the module

>>> from first import *

>>> myfun(2)

3

# Import multiple variables at once

>>> from first import a, myfun

>>> a

1

Copy the code

The package import

Create a new folder1 folder in the TryModule folder. We want folder1 to be a package. Create a new abcd.py file in this folder

b = 2

class Myclass:

def __init__(self, name, age):

self.name = name

self.age = age

def get_info(self):

print('my name is {name} and age is {age}'.format(name = self.name, age = self.age))

Copy the code

Create a new __init__.py file in the folder1 folder, otherwise the program will treat the folder as a normal folder instead of a package. The __init__.py file can be filled with nothing.

The file structure is as follows

trymodule

first.py

├ ─ ─ ─folder1

│ │ abcd.py

│ │ __init__.py

Copy the code

Again, open the command line in the tryModule folder and enter Python interactive mode

Let’s look at the following import methods

>>> import folder1

>>> folder1.abcd.b

Traceback (most recent call last):

File "<stdin>", line 1.in <module>

AttributeError: module 'folder1' has no attribute 'abcd'

>>> from folder1 import abcd

>>> bob = abcd.Myclass(name = 'Bob', age = 20)

>>> bob.name

'Bob'

>>> bob.get_info()

my name is Bob and age is 20

>>> from folder1.abcd import b

>>> b

2

>>> import folder1.abcd

>>> abcd.b

Traceback (most recent call last):

File "<stdin>", line 1.in <module>

NameError: name 'abcd' is not defined

>>> folder1.abcd.b

2

>>> import folder1.abcd as aa

>>> aa.b

2

Copy the code

Note:

  • However, the import package can not be used arbitrarily in the module, import to the specific module or variable level
  • It can be used between folders and files.You can also usefrom importFormat, and between the file and variables can only be usedfrom importFormat, that is, cannotimport folder1.abcd.b

special__init__.pyfile

The __init__.py file is actually a special file that corresponds to a module named folder1, meaning that variables defined in the __init__.py file can be called if you use import Folder1.

Write the __init__.py file as follows

from folder1.abcd import b

c = 3

Copy the code

Open the command line in the tryModule folder and enter Python interactive mode

>>> import folder1

>>> folder1.c

3

>>> folder1.b

2

>>> from folder1 import b

>>> b

2

Copy the code

In contrast to the previous from folder1.abcd import b, use the __init__.py file to import some commonly used variables for easy invocation.

There are two other points to note

  • __init__.pyWhen the file is written, if you want to import variables from other modules, even if__init__.pyFiles andabcd.pyThe files are in the same folderfrom abcd import bTo start from where the abcd file came from, that is, from the package name.
  • You do not need to create nested folders in the folder1 folder__init__.pyThe file can be called just like a module, but it is usually created so that you can easily import commonly used variables.

Search path for imported modules

When import Hello is used, Python searches the hello.py file in the following order

  • First search for built-in moduleshello(So we do not define the same module name as the built-in module)
  • If you don’t have built-in modules, look in these directories
>>> import sys

>>> sys.path

[' '.'C:\\Program Files\\Anaconda3\\python35.zip'.'C:\\Program Files\\Anaconda3\\DLLs'.'C:\\Program Files\\Anaconda3\\lib'.'C:\\Program Files\\Anaconda3'.'C:\\Program Files\\Anaconda3\\lib\\site-packages'.'C: \ \ Program Files \ \ Anaconda3 \ \ lib \ \ site - packages \ \ Sphinx - 1.4.6 - py3.5. Egg'.'C: \ \ Program Files \ \ Anaconda3 \ \ lib \ \ site - packages \ \ snownlp - 0.12.3 - py3.5. Egg'.'C:\\Program Files\\Anaconda3\\lib\\site-packages\\win32'.'C:\\Program Files\\Anaconda3\\lib\\site-packages\\win32\\lib'.'C:\\Program Files\\Anaconda3\\lib\\site-packages\\Pythonwin'.'C: \ \ Program Files \ \ Anaconda3 \ \ lib \ \ site - packages \ \ setuptools - 27.2.0 - py3.5. Egg']

Copy the code

The first ” represents the current working path. We can see that the path of the installed third-party packages (‘C:\\Program Files\\Anaconda3\\lib\ site-packages’) is also in this list, so packages can be found wherever the working path is.

If you want to add a search path, refer to this article

__all__

To be clear, import * cannot import variable names that begin with an underscore

The contents of the __init__.py file are changed as follows

from folder1.abcd import b

c = 3

_e = 4

Copy the code

In Python interactive mode

>>> from folder1 import *

>>> c

3

>>> _e

Traceback (most recent call last):

File "<stdin>", line 1.in <module>

NameError: name '_e' is not defined

Copy the code

If you specify an import, you can

>>> from folder1 import c

>>> c

3

>>> from folder1 import _e

>>> _e

4

Copy the code

If __all__ is defined, import * imports variables starting with an underscore

The contents of the __init__.py file are changed as follows

from folder1.abcd import b

__all__ = ['c', '_e']

c = b + 1

_e = 4

Copy the code

In Python interactive mode

>>> from folder1 import *

>>> b

Traceback (most recent call last):

File "<stdin>", line 1.in <module>

NameError: name 'b' is not defined

>>> c

3

>>> _e

4

Copy the code

Visible import * only imports variables specified in __all__, whether or not they start with an underscore. This restriction prevents the import * command from importing too many variables and contaminating the namespace, filtering out intermediate variables such as b

Absolute versus relative references

Imports in Python are divided into absolute and relative references. The difference between them is the way in which the referenced module is located

  • Absolute references are explicitly specified superlative files (folders) used between files.Connect, in turn, to the module to be referenced. All of our uses above are absolute references.
  • Relative references specify the location of the module to be referenced relative to the current file..Indicates the upper-level file

In this file structure

trymodule

first.py

├ ─ ─ ─folder1

│ │ abcd.py

│ │ __init__.py

Copy the code

Write an __init__.py file that references variables in the abcd.py file

  • Absolute reference isfrom folder1.abcd import b
  • Relative reference isfrom .abcd import b

In relative references,. Refers to the parent file (also used from. Import XXX),.xxx refers to the same layer file,.. XXX is the same as the parent folder XXX file (one more. One more layer up

Which one do you usually use?

Absolute references are officially recommended after Python3, and are advantageous only when the file relationships in the module are very complex.

Import Runtime nature

With import statements, make two things clear

(1) When executing the command of importing modules, it will first check whether the modules to be imported are among the existing modules. If so, the import will be skipped. So modules referring to each other do not cause an infinite loop.

Use the following method to view the currently imported modules

import sys

sys.modules

Copy the code

The result is a dictionary where the key is the module name and the value is the file path

(2) Import statement and file execution

In this file structure

trymodule

first.py

├ ─ ─ ─folder1

│ │ abcd.py

│ │ __init__.py

Copy the code

Folder1 is a package and abcd is a module

  • import folder1Just import package, which is equivalent to executing__init__.pyfile
  • from folder import abcdIs carried out__init__.pyDocuments documents andabcd.pyfile
  • from folder1.abcd import bIn fact, it was executed__init__.pyDocuments documents andabcd.pyfile

(To see what was executed, add a print statement to these files and see if the result is printed.)

Knowing this implementation principle makes it easier to understand some of the previous conclusions

  • The first is in__init__.pyWith nothing in the file,import folder1Can’t callabcdThe variable in the module is equivalent to running an empty file without importing the entire package into the workspace
  • abcdDefined in the moduleprintAfter the statement,importTwice. Only the first timeprintThe second check shows that the module is already in the import list, which is ignoredimportThe command

See this article for more operational details

if __name__ == '__main__'

We often find if __name__ == ‘__main__’ in other people’s code. To understand what this does, let’s look at the following example

Create a new new.py file under folder1

print(__name__)

Copy the code

Open the command line in folder1 and type

python new.py

Copy the code

The result is __main__

Open the command line in the tryModule folder and enter Python interactive mode

>>> from folder1 import new

folder1.new

>>> print(__name__)

__main__

>>> print(new.__name__)

folder1.new

Copy the code

The above test results show that there is a difference between a direct run file and an import file. The difference is in the __name__ variable. The __name__ variable is a special variable that is assigned to each py file at runtime and can be viewed even in interactive mode.

So it’s easy to understand why if __name__ == ‘__main__’ does not execute the following code when importing and only runs the following code when executing the file directly. This is a bit of a convention, so if you’re not afraid to import files, you don’t need to use it.

Welcome to my zhihu column

Column home: Programming in Python

Table of contents: table of contents

Version description: Software and package version description