Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of "Absolute Import" in Python 2.7

  • Python 2.7.10
  • In virtualenv
  • Enable from __future__ import absolute_import in each module

The 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.

  1. What is the correct way of "absolute import"? Do I have to add the path to Project to sys.path?
  2. How could I run test_module1.py in the interactive interpreter? By python prjt/pkg1/tests/test_module1.py or python -m prjt/pkg1/tests/test_module1.py?
like image 478
Zelong Avatar asked Nov 24 '15 12:11

Zelong


People also ask

What are the three types of import statement in Python?

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)

Should I use relative or absolute imports Python?

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.

How do I use Python import?

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.

What does absolute import do in Python 3?

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.

What are the rules about importing in 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.

How do I import a python function from another module?

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.

What is relative import in Python?

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 :


2 Answers

How python find module

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() 
like image 145
Sean Avatar answered Sep 20 '22 00:09

Sean


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.

like image 36
Vasily Ryabov Avatar answered Sep 23 '22 00:09

Vasily Ryabov