For example, we have a file with the following structure:
pkg/
__init__.py
libs/
some_lib.py
__init__.py
components/
code.py
__init__.py
if we want to call in
module, such as using relative call: code.py
libs/some_lib.pyfrom.. Libs.some_lib import something
, it is not enough simply to add /___ to the package. Py
. Python returns the error ValueError: first import in non-package
. So how do you solve this problem?
has the following solution:
Add the current path to sys.path
considering that compontent
and libs
are folders at the same level, we can add the following code directly in code.py
to add the parent folder of the current folder to the system path.
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
or the following (this is true for any relational folder, as long as we give the absolute path to the folder in lib_path
) :
import os, sys
lib_path = os.path.abspath(os.path.join('..'))
sys.path.append(lib_path)
so we can import something with from libs.some_lib import something
.
executes the code in package
mode:
python -m pkg.components.code
and then we can use from.. Libs.some_lib import something
to import.
note that you don’t need .py
to end the file.
summary h2>
we can actually combine these two approaches:
if __name__ == '__main__':
if __package__ is None:
import sys
from os import path
sys.path.append( <path to the package> )
from libs.some_lib import something
else:
from ..libs.some_lib import something
div>
Read More:
- Vs Code Python relative path error
- Relative path and absolute path${ pageContext.request.contextPath }
- From in Python__ future__ The role of import *
- An import error is reported in the python. The solution to setting. Pylintrc is invalid
- sys.path.append(), os.path.exists(), os.path.join() and os.makedirs() meaning
- How to solve the import failure by prompting beautifulsoup under pychar
- How to make the import complete smoothly!
- Solve the problem of prompt couldn’t find path to unrear library after installing unrear in Python
- How to Change Local Path in TFS
- numpy.core.umath How to solve the problem of failed to import
- How to Fix gdb error: Failed to import the site module,No module named ‘_sysconfigdata_m’
- Import error, failed to import the debug version of PYD
- Python dynamically imports objects, importlib.import_ Module() uses
- Python_ Part 2 programming problems (3)_ solve numpy.core.multiarray Failed to import problem
- Solution of OpenCV library import error in Python 3
- Module build failed: error:couldn’t find preset “env” relative to directory
- How to solve “import cv2 failed ImportError: DLL load fail: Cannot find the specified module” in Anaconda environment
- No code, a solution to the error in the path of reading CSV file by Python: filenotfounderror: [errno 2] no such file or directory: ‘XX. CSV‘
- Solve the problem of unable to locate package python3.6 when upgrading from python3.5 to python3.6 in ubantu16.04
- ClobberError: The package ‘xxx‘ cannot be installed due to a path collision for ‘xx‘ This path alre