1

I have some funny noob problem.

I try to run unit tests from commandline:

H:\PRO\pyEstimator>python src\test\python\test_power_estimator.py
Traceback (most recent call last):
  File "src\test\python\test_power_estimator.py", line 2, in <module>
    import src.main.python.power_estimator as power
ImportError: No module named src.main.python.power_estimator

this same happens when I try to run it in desired folder:

H:\PRO\pyEstimator\src\test\python>python test_power_estimator.py

My folder structure looks like this.

├───src
│   │   __init__.py
│   │   __init__.pyc
│   │
│   ├───main
│   │   │   __init__.py
│   │   │   __init__.pyc
│   │   │
│   │   └───python
│   │       │   __init__.py
│   │       │   power_estimator.py
│   │       │   __init__.pyc
│   │       │   power_estimator.pyc
│   │       │
│   │       └───GUI
│   │               __init__.py
│   │
│   └───test
│       │   __init__.py
│       │
│       └───python
│               test_power_estimator.py
│               __init__.py
│               covrunner.bat
│               .coverage
│
└───doc

Maybe i don't see something obvious. I also try to run coverage. Is this approach good (file structure) ?

1 Answer 1

1

The immediate issue you are facing is a misunderstanding of what is "local code" in Python (I am not sure if there is an official terminology, so I am making this one up) and how to import it.

When you run python src\test\python\test_power_estimator.py, the first element in sys.path is set to the directory containing the test_power_estimator.py script, not the current directory. So the statement "import src.main.python.power_estimator as power" looks for the package src in the directory src/test/python, and that fails.

One way to work around the issue is to set the PYTHONPATH environment variable to "H:\PRO\pyEstimator"

But the recommended way to run tests is to use a test runner script. I recommended using nosetest.

In addition, nosetest has support for collecting coverage data while running your tests.

Besides, it sounds like a bad idea to have a python package named "src". You should rename your package to be your project. Maybe "estimator" or "pyestimator" (lowercase, please).

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for reply, what i've done to launch this project is : import sys import os sys.path.append(os.getcwd()) from src.main.python.power_estimator import * But i don't like that. I'll try PYTHONPATH instead. "SRC" Is it really that bad, while my root package directory names "estimator" and inside I've all project files, docs, SConstructs etc..?
As long as there is a init.py file directly in the PyEstimator directory, that's mostly fine. The issue is that "src" is a name I would not put at the root of my module namespace. And either "src.main.python.power_estimator" is a relative import (fragile, bad!) or "src" is the package root name (kind of unhygienic).
Thanks again. I'll certainly follow your remarks. Do You know any good resource in the network for some pragmatic approach in this area? I was basing on this: docs.python.org/tutorial/modules.html#packages and as we see this is not exhausting the good practices.
PEP8 is a good baseline, but then a lot of details are a matter of judgement and experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.