0

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 by python 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?

Floella
  • 1,279
  • 1
  • 22
  • 41
  • 3
    You should make your libary *installable*, so you can `pip install root_project_folder` and then your library would work from anywhere. Then your tests are designed to be run in an environment that has installed the package. Then your CI/CD tooling can just `pip install` the package then run the tests – juanpa.arrivillaga Oct 18 '22 at 19:41
  • 1
    Package your software per setuptools documentation. After doing so, run `python setup.py develop` (following this documentation will have you create a `setup.py` file), and then `import folder` will refer to the tree where you ran that command. – Charles Duffy Oct 18 '22 at 19:41
  • 1
    ...mind, it's `python setup.py develop` (or `pip install --editable .`) for you as a developer; it's `pip install yourproject` for a user. – Charles Duffy Oct 18 '22 at 19:42
  • 2
    @CharlesDuffy although, setuptools based packaging now has been moving towards using `setup.cfg` + `pyproject.toml` (often just `pypoject.toml`) – juanpa.arrivillaga Oct 18 '22 at 19:44
  • 1
    @juanpa.arrivillaga, fair 'nuff. My day job hasn't been in the Python world for a while now; I'm increasingly out-of-date. – Charles Duffy Oct 18 '22 at 19:44
  • 1
    Take a look at [ImportError: No module named ](https://stackoverflow.com/q/43476403/674039). – wim Oct 18 '22 at 19:45
  • 2
    @Floella, ...so, if you do the above, you can even skip the "give a beginner a copy of this project" step -- `pip` itself will do the work of downloading the project from [pypi](https://pypi.org/). (Granted, things with C dependencies, or that depend on a very specific version of the Python interpreter, can be more work to package robustly -- personally, I use [Nix](https://nixos.org/) for the hard cases). – Charles Duffy Oct 18 '22 at 19:48
  • 1
    @CharlesDuffy just a suggestion, most projects that have been around still use a setup.py, even if it is just an empty one with a setup.cfg, but I figure if you are starting something new today may be best just to go with `pyproject.toml`. Then it cna work with `pip` or `poetry` for example, and it is pretty simple. But as always, the packaging ecosystem is a jungle in Python. But things are moving towards pyproject.toml, it [is supposed to unify things](https://peps.python.org/pep-0518/), that's the idea.... – juanpa.arrivillaga Oct 18 '22 at 19:52

0 Answers0