[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Import module from a different subdirectory

Rich Shepard <rshepard at appl-ecosys.com> writes:
> I'm developing a Python3 application using Python3-3.7.3 and
> virtualenv-16.5.0 on a Slackware-14.2 host.
> The project directory contains subdirectories, including gui/ (with the
> tkinter views) and classes/ with the SQLAlchemy model.py.
> Within the gui/ directory as the cwd testing the code for a view needs to
> import the 'model' module from the class/ subdirectory and I have not been
> able to use the correct syntax to import that module. The view module starts
> with:
> from classes import model as m
> import data_entry_validators
> from commonDlgs import LabelInput
> import tkinter as tk
> from tkinter import ttk
> from datetime import datetime
> When I try to display this UI view I get an error:
> $ 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'

Recently, there has been a long discussion in this list about a similar
topic. Use your favorit search engine to search for it.

The likely cause for your problem is that viewed from the location
of "test_act_de.py", "classes" is not a subdirectory.
Either put your scripts in the top level folder of your project
(such that "classes" and friends are subdirectories of the script
containing folder) or extend "sys.path" in your scripts (before
the imports) to contain this folder.