I'm trying to keep my project structure as clean as possible as it's intended to be used by beginners. That's also the reason why I'd like to find the less cumbersome way to execute it, and even better if it's cross-platform and if it applies to all Python 3.x versions (otherwise, I'd like to know what changes, so I can write a proper readme).
My project uses unittest as I want to keep it as simple as possible, and the structure is as follows:
root_project_folder
├─ folder
│ ├─ src
│ │ └─ mycode.py
│ └─ tests
│ │ └─ tests_for_code.py
└─ └─ run_tests.py
The run_tests.py file has the following contents:
import unittest
import tests.tests_for_code
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTests(loader.loadTestsFromModule(tests.tests_for_code))
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
The mycode.py file isn't complicated at all, just a couple functions with no external modules used (only what's inside the project plus unittest).
But I can't run the project consistently, as most alternatives give me a "No module named folder/run_tests" or "No module named folder" error.
So far, I've only been able to run it successfully in 2 ways:
- within Pycharm (no specific run configurations set, just the interpreter path in "Settings > Project > Python Interpreter"),
- within Bash if I cd into the root project folder and then enter
export PYTHONPATH="$PWD"
followed bypython folder/run_tests.py
. The thing is, export doesn't work for CMD, and also tried Visual Studio code with no luck, even when I have my Python folder (which is C:\Users\Myuser\AppData\Local\Programs\Python\Python39) set in the Windows PATH environment variable.
But other attempts have been unsuccessful. I've tried without setting PYTHONPATH and using the following variants:
python folder/run_tests.py
,python folder/run_tests
,python -m folder/run_tests.py
,python -m folder/run_tests
,- CD into folder and then
python run_tests.py
, - CD into folder and
python -m run_tests.py
- CD into folder and
python -m run_tests
None of them work. I keep getting the "no module named..." error. Also if I use Bash to CD into folder and then set PYTHONPATH there, this doesn't work either: python run_tests.py
I've read multiple threads with solutions that depend on the OS or the Python version or some other factors, but nothing simple where I can give a beginner a copy of this project and tell them "enter this command to run it". Is there really no way to do this? If not, what would be the cleanest and easiest way to run the project independently of the platform?