There are many suggestions on the web what the structure of the Python project can/shall be, e.g. What is the best project structure for a Python application?.
"proj-dir"
+- doc
+- apidoc
+- scripts
+- "src-dir"
+- tests
It seems that many people in the Python world prefer the "src-dir" and "proj-dir" to be equal (or very similar). However, the scripts and tests will surely have to import some modules from src-dir, so either I have to
Or, is there any other possibility which I overlook now?
Suppose I have such structure and have set up the PYTHONPATH correctly. Now I commit the project into VCS and another developer checks the project out. On his machine the PYTHONPATH will not be set properly and the imports in scripts and in tests will not work.
Even through project structure is complex and testcases uses src in different package, you can add PYTHONPATH in Environmental Variables in Windows
My Computer --> Properties --> System Properties --> Advanced Tab --> Environmental Variables. Add new System Variable PYTHONPATH and append your projects dir to that variable with delimiter ';'
I suggest you to keep readme.txt to your project which clearly says to how to add PYTHONPATH in order to run the scripts. It is as similar as adding PATH of android-sdk to make adb shell to work, adding Java path to make Java to work in command prompt
Thus readme.txt helps to run the script when downloaded in different machine with only one change(adding PYTHONPATH variable with value = projectpath)
No need to add all modules of src in order to make testcases to work which is using src
Here is an example proj structure:
SAMPLEPROJECT
src
com
sample
example
utils
a.py
b.py
testcases
test1.py
test2.py
res
docs
If test1.py test case is using a.py and and b.py: Dont add utils module to PYTHONPATH, as you already added project path to python path your import statement looks something like this in test1.py
from src.com.sample.example.utils import a.py
from src.com.sample.example.utils import b.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With