Marto Marto - 15 days ago 5
Python Question

Python does not recognize a module which is set by PYTHONPATH

I've searched for some time to find a solution to this problem but have come up dry. I'm hoping I can find some help here. As the title suggests, python is not recognizing a module that has been set in my

PYTHONPATH
.

I'm currently working on a project that has the following overall structure:

base
├── util
│   └── logging_utility.py
└── project
└── app
├── config.py
└── runner.py


This is executed in python 3.5 via a virtual environment called
venv_1
. I use a Mac running El Capitan.

My
runner.py
script calls the
logging_utility
using the following import statement:

from util.logging_utility import method
.

When executing, I receive this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'util.logging_utility'


I am running this in the terminal from the
base
level of the directory. I'm using the following command to call the script:

PYTHONPATH=$HOME/base VIRTUAL_ENV=$HOME/.virtualenvs/venv_1 PATH=$VIRTUAL_ENV/bin:$PATH $HOME/.virtualenvs/venv_1/bin/python -- project/app/runner.py


Now, I've tried to debug this by printing out my
env
as well as
sys.path
. In both cases, the
base
directory appears in the path. So why on earth am I receiving this error?

I've also tried to update the import statement to
from base.util.logging_utility import method
which gives me the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'base'


Any idea why this is happening and how I can correct this?

Thanks!

UPDATE

Thanks to Billy and Moinuddin Quadri whose suggestions solved the issue. For some reason I still needed to add the
__init__.py
files. Even though python 3.3+ supports implicit imports, looks like it was still needed. This solved the problem.

Answer

You might not be having the path to package in your PYTHONPATH. In order to add that, do:

 import sys
 sys.path.append('/absolute/path/to/base')

Since from Python 3.3, you do not need the __init__.py.

For the older version, the other possible reason is missing __init__.py. If you do not have a __init__.py file present in the directory, you can not import files of that directory.

As is mentioned in the Packages Document:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Comments