Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing a package from within another package in python

Please assume the following project structure:

/project
  /packages
    /files
      __init__.py
      fileChecker.py
    /hasher
      __init__.py
      fileHash.py
    mainProject.py
    /test

I would like to get access to the module fileChecker.py from within the module fileHash.py. This is some kind of global package.

One way is to append paths to sys.path. [Is this PYTHONPATH by the way?]

What would be a solution when distributing the project?

  • Same as above? --> But then there could be paths to modules with the same name in PYTHONPATH?
  • Is setuptools doing all the work?

How can I achieve it in a nice and clean way?

Thanks alot.


Update:

Also see my answer below --> when calling fileHash.py (including an import like from files import fileChecker) directly from within its package directory, the project's path needs to be added to sys.path (described below).

Test cases situated within /test (see structure above) also need the path added to sys.path, when called from within /test.

like image 647
rocksteady Avatar asked Nov 15 '15 19:11

rocksteady


People also ask

Can a package be imported in Python?

While importing packages, Python looks in the list of directories defined in sys. path , similar as for module search path.


1 Answers

Thanks mguijarr.

I found a solution here on stackoverflow: source: How to fix "Attempted relative import in non-package" even with __init__.py

when I am in the project folder /project, I can call the module like this:

python -m packages.files.fileHash (no .py here, because it is a package)

This is wokring well. In this case PYTHONPATH is known and the import can look like this:

from packages.files import fileChecker

If it is not called directly, but from within the package directory in my case /packages/hasher --> setting the PYTHONPATH is needed:

if __package__ is None:
    import sys
    from os import path
    sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
    from packages.files import fileChecker
else:
    from packages.files import fileChecker

The important thing for me here is, that the path to include is the PROJECT path.

The code snippet above(the last one) already includes the case describes both cases (called as package and directly).

Thanks alot for your help.

Update:

  1. Just to make my answer more complete

Python adds the current path to the PYTHONPATH automatically when doing

python fileHash.py

Another option, in addition to the one above, is to set the PYTHONPATH when running the program like this

PYTHONPATH=/path/to/project python fileHash.py
  1. I gained some experience, I would like to share:

    • I don't run modules from within their directories anymore.
    • Starting the app, running tests or sphinx or pylint or whatever is all done from the project directory.
    • This ensures that the project directory is contained in the python path and all packages, modules are found without doing additional stuff on imports.
    • The only place I still set the python path to the project folder using sys.path is in my setup.py in order to make codeship work.

Still, in my opinion this is somehow not an easy matter and I find myself reflecting the PYTHONPATH often enough :)

like image 169
rocksteady Avatar answered Oct 05 '22 20:10

rocksteady