Resolving import errors in Python

If you get an error saying something like ‘Can not import SomeModule”, the first thing to check is that package (which is a regular directory with added __init__.py file) is in PYTHONPATH.
For example, suppose you get an error when executing
from util import SafeMath

I would open python shell to check if its in PYTHONPATH and then to see which modules/files are seen by interpreter in case of potential name conflicts:
python

import sys
sys.path

The above will display all directories in PYTHONPATH.
import util
help(util)

The above will display all files/modules found in the package

Advertisements
This entry was posted in Python. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s