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:
- Python Import Error: SystemError: Parent module ‘‘ not loaded, cannot perform relative import
- [Solved] Python Relative Reference Error: ImportError: attempted relative import with no known parent package
- [Solved] SystemError: Parent module ” not loaded, cannot perform relative import
- [Solved] Python Project Import Module Error: ModuleNotFoundError
- Solution for Python3.7 import gevent module error
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- [Solved] python3.10 Error: cannot import name ‘Iterable‘ from ‘collections‘
- Change the Python installation path in Pycharm
- [Mac Pro M1] Python3.9 import cv2 Error: Reason: image not found
- [Solved] pycharm Import New Project Error: cannot set up a python sdk
- How to Solve Python ImportError: cannot import name UnrewindableBodyError
- [Solved] Python 3.7 from collections import Iterable Error
- Full explanation of SYS module of Python
- Python: How to Use os.path.join()
- [Solved] From pip._internal import cmdoptions ImportError: cannot import name SourceDistribution
- from keras.preprocessing.text import Tokenizer error: AttributeError: module ‘tensorflow.compat.v2‘ has..
- Command “/usr/bin/python -u -c “import setuptools, tokenize;__file__=‘/tmp/pip-cus9V0-build/setup.py
- Python failed to import pyx file [How to Solve]
- [example multitasking] Python multithreading module
- [Mac M1] How to Solve import wordcloud Error: ModuleNotFoundError: No module named ‘wordcloud‘