views:

94

answers:

1

Hi, 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  A: 

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).

ddaa
Hi, thanks for reply, what i've done to launch this project is :import sysimport ossys.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..?
bua
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).
ddaa
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: http://docs.python.org/tutorial/modules.html#packages and as we see this is not exhausting the good practices.
bua
[PEP8](http://www.python.org/dev/peps/pep-0008/) is a good baseline, but then a lot of details are a matter of judgement and experience.
ddaa