Search code examples
python-3.xpython-modulepython-packaging

Import a python module from within a packed module


So I builded a python package localy:

cgi@cgires:~$ pip list | grep mads
madscgi                     0.1.0

Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:

working_outside_the_packaged code

Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:

working_packaging

So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.

Is is possible to enforce the usage of the installed pip-package?


Solution

  • As @MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.

    Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.