Import module from a different subdirectory
Peter J. Holzer
hjp-python at hjp.at
Sat May 18 12:22:19 EDT 2019
On 2019-05-18 05:45:23 -0700, Rich Shepard wrote:
> On Sat, 18 May 2019, dieter wrote:
> > > > 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']
>
> All directories are present, and the one for the current project is the
> first one searched.
[...]
> I understand this. Within ~/development/bustrac as the CWD when I invoke the
> test application with the first line:
>
> from classes import model as m
>
> I still see this result:
>
> $ python3 test_act_de.py Traceback (most recent call last):
> File "test_act_de.py", line 1, in <module>
> from classes import model as m
> ModuleNotFoundError: No module named 'classes'
>
> 'classes' is a package (a subdirectory) containing an empty __init__.py, not
> a module. 'classes' conains a single module, model.py.
This works for me:
hrunkner:~ 18:14 :-) 1141% cd tmp
hrunkner:~/tmp 18:14 :-) 1142% mkdir bustrac
hrunkner:~/tmp 18:14 :-) 1143% cd bustrac
hrunkner:~/tmp/bustrac 18:14 :-) 1144% mkdir classes
hrunkner:~/tmp/bustrac 18:14 :-) 1145% touch classes/__init__.py
hrunkner:~/tmp/bustrac 18:14 :-) 1146% touch classes/model.py
hrunkner:~/tmp/bustrac 18:14 :-) 1147% python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
>>>
"" is in sys.path, so "classes" and classes.model are found.
Now lets go to a different subdirectory:
hrunkner:~/tmp/bustrac 18:15 :-) 1148% mkdir scripts
hrunkner:~/tmp/bustrac 18:15 :-) 1149% cd scripts
hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1150% python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'classes'
>>>
This doesn't work, since there is no classes/model.py in "", only in "..".
But if I add a PYTHONPATH, it works again:
hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1151% export PYTHONPATH=~/tmp/bustrac
hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1152% python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
>>>
--
_ | Peter J. Holzer | we build much bigger, better disasters now
|_|_) | | because we have much more sophisticated
| | | hjp at hjp.at | management tools.
__/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190518/61ed8e46/attachment.sig>
More information about the Python-list
mailing list