Reputation: 313
I have a Python directory like this, and I am trying to import classes from main_script.py
.
- main_folder
- main_script.py
- resources
- package_folder
- class_files
- class1.py
- class2.py
- class3.py
- __init__.py
This is what I tried
sys.path.append('../resources/package_folder)
from class_files import *
But I get this error, "No module named 'class_files'"
Upvotes: 1
Views: 50
Reputation: 13079
You have a relative path in your sys.path
which is relative to the process's current directory rather than the location of the main_script.py
script itself.
You can use instead:
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__),
'../resources/package_folder'))
from class_files import *
or if you prefer:
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__)),
'resources/package_folder'))
Separately, you might also find that in your __init__.py
you need:
__all__ = ['class1', 'class2', 'class3']
Upvotes: 1