from __future__ import absolute_import
in each moduleThe directory tree looks like:
Project/ prjt/ __init__.py pkg1/ __init__.py module1.py tests/ __init__.py test_module1.py pkg2/ __init__.py module2.py tests/ __init__.py test_module2.py pkg3/ __init__.py module3.py tests/ __init__.py test_module3.py data/ log/
I tried to use the function compute()
of pkg2/module2.py
in pkg1/module1.py
by writing like:
# In module1.py import sys sys.path.append('/path/to/Project/prjt') from prjt.pkg2.module2 import compute
But when I ran python module1.py
, the interpreter raised an ImportError that No module named prjt.pkg2.module2
.
Project
to sys.path
?test_module1.py
in the interactive interpreter? By python prjt/pkg1/tests/test_module1.py
or python -m prjt/pkg1/tests/test_module1.py
?There are generally three groups: standard library imports (Python's built-in modules) related third party imports (modules that are installed and do not belong to the current application) local application imports (modules that belong to the current application)
Note that relative imports are based on the name of the current module. Since the name of the main module is always “main”, modules intended for use as the main module of a Python application must always use absolute imports.
Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.
tl;dr: Importing absolute_import from the __future__ module changes the behavior of implicit relative imports within a Python 2 program. Since Python 3 disallows that type of import, they must be eliminated from code that is to run under both versions of Python.
Some rules about importing. Import statements should be mentioned at the top of the files using those statements. The order in which the import has to be mentioned is as below. In this type of import, we specify the full path of the package/module/function to be imported. A dot (.) is used in pace of slash (/) for the directory structure.
Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. Import statement consists of the import keyword along with the name of the module.
Relative import specifies object or module imported from its current location, that is the location where import statement resides. There two types of relative imports : Implicit relative imports : Implicit relative import have been disapproved in Python (3.x). Explicit relative imports :
python will find module from sys.path
, and the first entry sys.path[0]
is '' means, python will find module from the current working directory
import sys print sys.path
and python find third-party module from site-packages
so to absolute import, you can
append your package to the sys.path
import sys sys.path.append('the_folder_of_your_package') import module_you_created module_you_created.fun()
export PYTHONPATH
the PYTHONPATH will be imported into sys.path before execution
export PYTHONPATH=the_folder_of_your_package import sys [p for p in sys.path if 'the_folder_of_your_package' in p]
How could I run test_module1.py in the interactive interpreter? By python Project/pkg1/tests/test_module1.py or python -m Project/pkg1/tests/test_module1.py?
you can use if __name__ == '__main__':
idiomatic way, and use python Project/pkg1/tests/test_module1.py
if __name__ = '__main__': main()
If you add sys.path.append('path/to/Project')
into prjt/__init__.py
, you need to import submodules so: from pkg2.module2 import compute
(without prjt
specification) because prjt
package import is in progress and the higher level folder is not in the sys.path
. This is exactly what @Cissoid suggested.
BTW, from __future__ import absolute_import
is not necessary for Py >= 3.0.
Answering your second question... I have very similar structure for unittests subfolder, so in the pkg/unittests/testall.py
I wrote the following:
testfolder = os.path.abspath(os.path.dirname(__file__)) project_root = os.path.abspath(os.path.join(testfolder, r"..\..")) sys.path.append(project_root) import pkg
I run it without -m
option because it's unnecessary complication.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With