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', )
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:If you do then the names listed in
__all__
are those that will be available after afrom 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:
or
or
Note that in the all above examples
functionA
has access to other functions withinfile_io.py
and, iffile_io.py
has the appropriate imports, to other functions in other files inmy_utils
.It is also important to remember that python is case dependent even on windows.