Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a module from a directory (package) one level up

Tags:

python

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

The following works just fine, but that is just a hack. I'd like a recommended / pythonic way.

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

The directory structure:

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

contents of fruit.py

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

contents of sub_pkg/recipe.py .. just a single import line:

from fruit import Fruit

When I run :

python recipe.py

it 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 did not work.

like image 273
stackjs Avatar asked Sep 24 '15 17:09

stackjs


People also ask

How do I import a module from one package to another?

To import a module from another path, you first need to import the sys module as well as any other Python modules that you would like to use in your program. The sys module is provided by the Python Standard Library and it provides functions and parameters that are system-specific. The path.

How do I import a module from the root directory?

In order to import a module, the directory having that module must be present on PYTHONPATH. It is an environment variable that contains the list of packages that will be loaded by Python. The list of packages presents in PYTHONPATH is also present in sys. path, so will add the parent directory path to the sys.

How do I import one module to another in Python?

Import in python is similar to #include header_file in C/C++. 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.


1 Answers

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

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

Or use a relative path

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

This should allow importing pkg1 from any where in your program.

There is always the option of using relative imports as suggested here, I find using absolute imports more readable and less likely to lead to bugs. Also, in large projects using relative imports will have you constantly calculating path hierarchies while when using absolute imports, it's simple to know you always refer to one root directory.

About relative imports from PEP328 in Python 2.5:

Reading code which relies on relative imports is also less clear, because a reader may be confused about which module is intended to be used. Python users soon learned not to duplicate the names of standard library modules in the names of their packages’ submodules, but you can’t protect against having your submodule’s name being used for a new module added in a future version of Python.


Guido is suggesting using leading dots in relative imports to avoid ambiguous imports as described above, from PEP328 again:

Guido has Pronounced that relative imports will use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots give a relative import to the parent(s) of the current package, one level per dot after the first.

like image 70
Forge Avatar answered Oct 30 '22 09:10

Forge