1

This is a broad question because no one seems to have found a solution to it as yet so I think asking to see a working example might prove more useful. So here goes:

Has anyone run a nosetests on a python project using imports of multiple files/packages?

What I mean is, do you have a directory listing such as:

project/
    |
    |____app/
          |___main.py
          |___2ndFile.py
          |___3rdFile.py
     |____tests/
          |____main_tests.py

Where your main.py imports multiple files and you perform a nosetests from the project file of utilizing a test script in the main_tests.py file? If so please can you screen shot your import section both of all your main files and your main_tests.py file?

This seems to be a major issue in nosetests, with no apparent solution:

Community
  • 1
  • 1
Jeeves
  • 424
  • 1
  • 7
  • 25
  • 1
    This is your second question about the same topic. I had asked you two questions you did not address the one that might most likely be the reason for the failure. Refer back to your question and look at the comments again. You shouldn't post the same question twice. But, this is a very common thing that does work. – idjaw Mar 31 '16 at 04:27
  • True same topic, different approach. – Jeeves Mar 31 '16 at 04:41

1 Answers1

2
  1. You can't have python modules starting with a digit, so 2ndFile.py, 3rdFile.py won't actually work (rename them).
  2. You'll need an __init__.py inside the app directory, for it to be considered a package, so add that (it can be empty file).
  3. You don't need an __init__.py in the tests directory!
  4. The import statements in main_tests.py should look like from app.main import blah
  5. The absolute path of the project directory needs to be in your sys.path. To achieve this, set an environment variable: export PYTHONPATH=/path/to/project

Now running nosetests should work.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Hi thanks, I removed the __init__.py and added the from app.main import blah as suggested and reran nosetests-- no errors. However when I run python app.py now, I am getting the error that I was getting for nosetests, ImportError: No module named ex48.2ndFile – Jeeves Mar 31 '16 at 05:29
  • Read again point 1. You can't have a module called 2ndFile.py – wim Mar 31 '16 at 15:43
  • The filenames are just examples--none in the actual directory start with numbers. Also, where exactly do you set the absolute path of the project directory, ie. export PYTHONPATH=/path/to/project--- is this inside the *.py file or an environmental variable on the system itself? – Jeeves Mar 31 '16 at 15:47
  • An environment variable on the system itself. Don't try to set it from within python. You'll need it for running your app aswell. – wim Mar 31 '16 at 15:48