Import module from a different subdirectory

Rich Shepard rshepard at appl-ecosys.com
Sat May 18 12:43:34 EDT 2019


On Sat, 18 May 2019, Peter J. Holzer wrote:

> "" is in sys.path, so "classes" and classes.model are found.
>
> Now lets go to a different subdirectory:
> This doesn't work, since there is no classes/model.py in "", only in "..".
>
> But if I add a PYTHONPATH, it works again:

Peter,

The project layout, briefly, is:

~/development/business-tracker/
 	classes/
 	gui/

All subdirectories contain a __init__.py file to identify them as packages.
'classes/' contains model.py; 'gui/' contains test_act_de.py.

The project root directory is present in both PYTHONPATH and sys.path:

$ echo $PYTHONPATH
/home/rshepard/development/bustrac/

$ python3
Python 3.7.3 (default, Mar 26 2019, 06:40:28) 
[GCC 5.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/lib/python3.7/site-packages']

When the gui/ subdirectory is the current working directory and I try to run
text_act_de.py I get the error that model.py is not found in the classes
subdirectory. I don't see how my directory structure, file locations, and
paths differ from your examples.

Regards,

Rich



More information about the Python-list mailing list