[Tutor] Importing a class from a sub-directory

Phil phillor9 at gmail.com
Fri Feb 9 20:58:11 EST 2024


Thank you for reading this and this is a problem that I've struggled 
with for quite some time. The simple fix was to copy myclass into 
whatever project directory that I was working on at the time, but of 
course, this means that I have to maintain multiple copies of the same 
class.

Say I have myclass.py saved in ~/Python and I want to import myclass 
into myproject.py that is saved in ~/Python/Tkinter/myproject. How do I 
do that?

I have tried countless possibilities including:

from ..myclass import Myclass

I've created a file named "__init__.py and saved myclass.py into that file

I've tried from "~/Python/myclass" import Myclass but that's a syntax error

I've scoured the Internet looking for a solution and read several 
tutorials and even consulted my AI friends and I'm still at a lose to 
solve this seemingly simple problem.

Using the tree command I have confirmed that I have the following 
directory structure:

~/Python
|__ myclass.py
|__ Tkinter
     |_ myproject.py

-- 

Regards,
Phil



More information about the Tutor mailing list