0

I have directory which has a app and it has corresponding test script files in 'tests' directory.

project/
--myapp/
--__init__.py(<-- updated)
--tests/
  --tests1.py
  --tests2.py etc
--run_tests.py

runtests.py traverses 'tests' directory recursively and executes all python scripts in it.

tests*.py has to import myapp for the tests to run. Since, tests*.py files are in 'tests' directory, it doesn't work.

How do I make 'myapp' to be available to all tests*.py files in 'tests' directory? I think there would be simple solution than setting 'myapp' in PYTHONPATH.

Note: nosetests tests/ will work. So, just curious on how it works.

Thanks in advance.

rajpy
  • 2,436
  • 5
  • 29
  • 43

2 Answers2

1

if myapp is in pythonpath:

from myapp import mymodule

You can also do relative imports:

from .. import mymodule
gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • Hey..sorry \_\_init\_\_.py is there in 'myapp' directory, otherwise even app will not work.:) And 'myapp' is not there in pythonpath. My question is 'nosetests tests/' will work. why not 'python runtests.py' – rajpy Jun 07 '13 at 15:21
  • added example for relative imports to my answer, does that help you? – gitaarik Jun 07 '13 at 16:00
  • Tried 'from .. import myapp', got error ValueError: Attempted relative import in non-package. – rajpy Jun 07 '13 at 16:36
  • From what file are you doing this? – gitaarik Jun 07 '13 at 16:45
0

Python has a feature that makes a directory a package (init.py). With that, each .py file becomes a part of this module.

try checking this post

What is __init__.py for?

Community
  • 1
  • 1
Adriano Almeida
  • 5,186
  • 5
  • 20
  • 28
  • Sorry.. I haven't mentioned \_\_init\_\_.py in question, but it is there, without that app will not work!:) – rajpy Jun 07 '13 at 15:22