Import a module from the catalog (package) one level up

What is the correct way in Python to import a module from a directory one level up? The directory is a Python package with all of these modules, and I have an auxiliary directory with code that needs these modules.

The following works are just fine, but it's just a hack. I would recommend / pythonic.

import sys sys.path.append("../") from fruit import Fruit print("OK") 

Directory structure:

 pkg1 __init__.py fruit.py +sub_pkg __init__.py recipe.py 

fruit.py content

 class Fruit: def get_name(self): print("Fruit name") 

the contents of sub_pkg/recipe.py .. only one import line:

 from fruit import Fruit 

When I run:

 python recipe.py 

he gives the following error.

 Traceback (most recent call last): File "recipe.py", line 2, in <module> from fruit import Fruit ImportError: No module named fruit 

I also tried: from pkg1.fruit import Fruit , does not work. Also looked at other similar questions .. python -m recipe.py or python -m sub_pkg/recipe.py does not work.

+6
source share
4 answers

In your main recipe.py file add the path pkg1 to PYTHONPATH

 sys.path.append('/path/to/pkg1') 

Or use a relative path

 sys.path.append('../..') 

This should allow you to import pkg1 from anywhere in your program.

It is always possible to use relative imports, as suggested here; I find using absolute imports more readable and less likely to lead to errors. In addition, in large projects using relative imports, you will constantly compute path hierarchies, and when using absolute imports, just know that you always refer to the same root directory.

About relative imports from PEP328 in Python 2.5:

Relative import-based reading code is also less clear, as the reader may be confused about which module is intended for use. Python users soon learned not to duplicate the names of standard library modules in the names of their package submodules, but you cannot protect against using the submodule name for a new module added in a future version of Python.


Guido suggests using leading points in relative imports to avoid ambiguous imports, as described above, from PEP328 :

Guido said that relative imports will use leading points. One leading point indicates relative imports starting from the current package. Two or more leading points give relative imports to the parent (s) of the current package, one level per point after the first.

+2
source

If you are having problems, you can also use the following.

This import from the current directory

 from . import something 

this import from one directory above

 from .. import fruit 

Doc for relative path: https://docs.python.org/2/tutorial/modules.html#intra-package-references

RELATIVE PACKAGE ISSUE ValueError: Attempting to relative import in a non-packet

For people having problems with the relative package. Here is your solution.

 if __name__ == "__main__" and __package__ is None: __package__ = "expected.package.name" 

Also explanation copied from another python docs

If the main module is indicated by its name, then the package attribute will be set to None. To enable relative imports when a module is executed directly, the following template is required for the first relative import statement:

 if __name__ == "__main__" and __package__ is None: __package__ = "expected.package.name 

Please note that this template template is sufficient only if the top-level package is already available through sys.path. The additional code that sys.path manages will be required for direct execution to work without the top-level package already being imported.

Doc for creating packages I am not going to embed content since it is quite long, but here is a section on python docs for creating packages. https://docs.python.org/2/tutorial/modules.html#packages

Another PYTHONPATH document https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

+2
source

You can use relative imports :

 from ..fruit import Fruit 
0
source

Instead of using top import, enter an entry point that you can call from the very top level and import it relative to that entry point in the recipe.py file.

Example:

 pkg1/ __init__.py main.py fruit.py +sub_pkg __init__.py recipe.py 

Where main.py contains:

 #!/usr/bin/env python3 import subpkg.recipe as recipe import fruit if __package__ is None and __name__ == "__main__": __package__ = "main" print() # Call recipe.py or fruit.py methods from here 

And in recipe.py :

 import fruit # do stuff with fruit module, which is properly imported now 

To start, call python3 main.py

-1
source

All Articles