Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python does not recognize a module which is set by PYTHONPATH

Tags:

python

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.

like image 882
Marto Avatar asked Nov 18 '16 18:11

Marto


People also ask

Why won't Python recognize my module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do you assign a path to a Python module?

Place module.py in the folder where the program will execute. Include the folder that contains the module.py in the PYTHONPATH environment variable. Or you can place the module.py in one of the folders included in the PYTHONPATH variable. Place the module.py in one of the installation-dependent folders.


1 Answers

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.

like image 175
Moinuddin Quadri Avatar answered Oct 26 '22 23:10

Moinuddin Quadri