directory structure
.
├ ─ ─ apt_root. Py
├ ─ ─ just set p y
├ ─ ─ mod/
└ ─ ─ test. Py
└ ─ ─ just set p y
└ ─ ─ sub/
└ ─ ─ test. Py
└ ─ ─ just set p y p>
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
p>
if h1>
. 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
p>
Analytical h1>
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.
p>
summary h1>
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
p>
p>
[welcome to follow my WeChat official number: artificial intelligence Beta]
Read More:
- Design python+mysql database with multi-level menu directory
- An introduction to sys modules in Python and how packages are imported and used
- Python recursively traverses all files in the directory to find the specified file
- Full explanation of SYS module of Python
- ModuleNotFoundError: No module named ‘tensorflow.python’ And the pits encountered after installation
- The PIP installation package was successful but the import failed
- How to Fix Errors encountered in executing Python scripts with command line parameters
- The Python DOM method iterates over all the XML in a folder
- npm install Error: stack Error: Can’t find Python executable “python”
- Python exports the gitlab project
- Linux Fatal Python error: Py_Initialize Unable to get the locale encoding
- Python FileNotFoundError: [Errno 2] No such file or directory: ‘objects/epsilon.pkl
- Change the Python installation path in Pycharm
- Error in sitecustomize set PYTHONVERBOSE for traceback KeyError: ‘PYTHONPATH’
- How to Fix keyerror in Python dictionary lookup
- Django uses Mysql to report an error loading MySQL DB module: no module named ‘mysqldb’
- Python asynchronous execution library asyncio
- linux ubuntu pip search Fault: <Fault -32500: “RuntimeError: PyPI‘s XMLRPC API is currently disab
- Django Issues: TypeError: “Settings” object is irreversible
- Python error collection: NameError: name ‘numpy’ is not defined