Importing module from another subdirectory
Sayth Renshaw
flebber.crue at gmail.com
Wed Apr 17 19:35:27 EDT 2019
On Thursday, 18 April 2019 06:59:43 UTC+10, Rich Shepard wrote:
> What is the proper syntax to import the model class in the model/
> subdirectory into a tkinter view module, e.g., activities.py? The syntax,
> 'import model as m' fails because it is not in the same subdirectory as the
> importing module.
>
> The program directory tree is:
>
> bustrac/
> README.rst
> bustrac.py*
> controller/
> model/
> scripts/
> views/
>
> Running pdb in python3-3.7.3 produces the same error:
> $ 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 pdb
> >>> import activities_data_entry
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/rshepard/development/business_tracker/views/activities_data_entry.py", line 1, in <module>
> import model.activities as act
> ModuleNotFoundError: No module named 'model'
>
> My previous use of python has had all files in the same directory so I've
> not before had to learn how to address this issue. Pointers appreciated.
>
> Regards,
>
> Rich
Morning
Apologies I don't know the answer but went looking. This guide should answer the question. Didn't know it was so difficult to be honest.
https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html#example-directory-structure
Then there was this rather long list of traps.
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
Most guides say it was fixed in 3.3 but still seems quite confusing post 3.3
Cheers
Sayth
More information about the Python-list
mailing list