Reputation: 3631
Hello fellow pythonistas,
I have an extended hierarchy of folders and subfolders of python scripts. From any script, I need to be able to import any other python script inside any of these other folders. I created the folders as packages because that was the recommended way for importing on many websites.
A first "guerilla" way that I implemented and it works is the following: 1. I created a paths.py file where I append to sys.path every new folder.
import sys
import platform
if platform.system() == 'Linux':
python_path = '/var/lib/jenkins/jobs/QA/workspace/Site'
else:
python_path = 'C:/Python27/projects/QA/Site'
#ACADEMIES
sys.path.insert(1, python_path + '/Academies/Tests')
sys.path.insert(2, python_path + '/Academies/Suites')
sys.path.insert(3, python_path + '/Academies/inc')
sys.path.insert(35, python_path + '/Academies/Academy_wall')
2.Inside every file I do the importing like this:
As you can see PyCharm is complaining about the imports; however when I run it it works.
Could it be possible to have a paths.py
file that imports all the different packages in there and for all the other scripts to just call import paths
and then from there import only the different files that I need like I do now?
Essentially, I want to do the same thing I'm doing in a more elegant and clear way.
Thanks in advance!
Upvotes: 4
Views: 983
Reputation: 315
Well since all of the modules in these folders appear to depend on each other, then I would suggest making this entire set of folders a giant package, like so:
base
|
+--Academies
|
+- __init__.py
+- FolderA
| +- __init__.py
| +- moduleA.py
| +- moduleB.py
+- FolderB
| +- __init__.py
| +- moduleA.py
| +- moduleB.py
In each of these modules, refer to other modules like so:
# File: Academies/FolderA/moduleA.py
from Academies.FolderB import moduleA, moduleB
from . import moduleB as local_b # moduleB is a naming conflict in this example so we rename it to local_b for the scope of this file.
With this scheme, you can still reference modules from any other module with relative ease.
If you need to run a module as __main__
, you'll have to modify the way you call them.
With the folder above Academies (base in this example) as your working directory, you'll want to call moduleA in folderB like so:
C:\path\to\base>python -m Academies.FolderB.moduleA
Upvotes: 1