a.powell
a.powell

Reputation: 1722

Import class from Python file in another folder -- not a child folder

Here is the structure of my directory:

MyFolder
   -run.py
SecondFolder
   -class.py

What I have attempted is adding the directory path to sys.path within the run.py file and this will work only occasionally (not sure why):

import sys
sys.path.insert(0, '/Users/.../SecondFolder/class.py')
from class import Connection

How can I ensure the module is always loaded? Any help is greatly appreciated.

Upvotes: 0

Views: 47

Answers (1)

Benjamin Ju
Benjamin Ju

Reputation: 85

If SecondFolder doesn't have to be in the same location as MyFolder, you can add it to the python site-packages.

From there, you can import it as so:

from SecondFolder.class import Connection

Upvotes: 1

Related Questions