ImportError persists after adding the path to PYTHONPATH

805 views Asked by At

I get the following error while running some python code

Traceback (most recent call last): File "./dspl.py", line 4, in import base ImportError: No module named base

The disp.py is in directory PERSISTENCE_LENGTH, as listed below. The disp.py imports few python scripts that are inside the directory UTILS (shown below). I added the path of imported directory (/home/vinay/oxDNA) to PYTHONPATH i.e.,export PYTHONPATH=${PYTHONPATH}:/home/vinay/oxDNA/). There is a proper__init__.py file inside the UTILS directory.

disp.py is in the directory: /home/vinay/oxDNA/EXAMPLES/PERSISTENCE_LENGTH

disp.py is importing other modules that are in the directory: /home/vinay/oxDNA/UTILS

When I print sys.path, I can see that PYTHONPATH is okay. as shown below ['', '/home/vinay', '/home/vinay/oxDNA/UTILS', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', )

1

There are 1 answers

1
Steve Barnes On

If your module is in a directory, rather than being a singled named file, then the directory is required to have a __init__.py file. The existence of this file makes the directory a module and then you can load from that module, the __init__.py can be empty but you can also have an entry in it of:

__all__ = ['component_name1', 'etc', 'etc']

If you do then the names listed in __all__ are those that will be available after a from mondule_name import *

The normal practice is to have a meaningful name for the directory, e.g.: `my_utils' and for the components within the directory, e.g.: 'file_io.py' and you can then access the items within file_io as:

import my_utils
my_utils.file_io.functionA()

or

from my_utils import file_io
file_io.functionA()

or

from my_utils.file_io import functionA()
functionA()

Note that in the all above examples functionA has access to other functions within file_io.py and, if file_io.py has the appropriate imports, to other functions in other files in my_utils.

It is also important to remember that python is case dependent even on windows.