Python 3 uses the relative path import module

directory structure

.
├ ─ ─ apt_root. Py
├ ─ ─ just set p y
├ ─ ─ mod/
└ ─ ─ test. Py
└ ─ ─ just set p y
└ ─ ─ sub/
└ ─ ─ test. Py
└ ─ ─ just set p y

task 1: import apt_root.py

from the parent directory in mod/test.py

task 2: import the sub/test.py

from the parent directory in mod/test.py

if

. Why does the title restrict the import of python3?

because all the peps you can find on the web are python2. Such as PEP328. But as far as I can see, python2 and python3 have different import rules.

absolute path is not good, why restrict to relative path import module?

refers to the module through the absolute path, which can easily cause a lot of work when the code structure is changed later, or when the file is renamed. Relative paths don’t have this problem


Analytical

one of the starting points of this article is that I found import is not easy, at least it caused a lot of confusion for me, so I share it here, hoping that the above two tasks can cover all the difficult cases. The first is the confusion of executing test.py in different ways, where the import is found to correspond to the module.

in the python form of test.py

in this case, python mod/test.py,

are executed in the root directory

or enter the mod subdirectory and execute python test.py with the same effect.

:

:

from . import apt_root
# 或者
from .. import apt_root
# 或者
from ..apt_root import *

I tested the successful way of writing:

import sys

sys.path.append(".")
import app_root

therefore, there should be one ‘.’ for the next level, and two ‘.’ for the next level. This means to add the previous directory to the search path.

in python-m test mode

, if my import is

import app_root

(as opposed to direct python xxx.py) runs in different directories and has different effects!

1: in the root directory: python-m mod. Test — run successfully

two: enter mod subdirectory first, then python -m test – run failure

if you want it to run successfully, it should look like this:

sys.path.append("..")
import app_root

(another confusing example) python-m XXX, to add the parent directory to the search path, use “..” , unlike python xxx.py, which USES “.” to represent the parent directory!

because python-m adds the path of the current command to sys.path. See python: The Python-m parameter?

therefore, in this method, it is necessary to combine the path of the current command running + the search path in the default sys.path + the newly added path in the code sys.path.append to determine whether the import can be successful.

summary

where it can be confusing:

1. Relative path cannot be used from.. To import XX, use sys.path.append(“..” )

2. Python-m XXX and python xx.py are different in the representation of the parent directory of import, the former USES two dots, the latter USES one;

3. The import search path in python-m XXX is related to the directory where the command is currently executing;

Python xxx.py is independent of the directory in which the command is currently executing


[welcome to follow my WeChat official number: artificial intelligence Beta]


Read More: