rajpy
rajpy

Reputation: 2476

Import error while running python scripts inside a directory?

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.

Upvotes: 0

Views: 110

Answers (2)

gitaarik
gitaarik

Reputation: 46350

if myapp is in pythonpath:

from myapp import mymodule

You can also do relative imports:

from .. import mymodule

Upvotes: 1

Adriano Almeida
Adriano Almeida

Reputation: 5356

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?

Upvotes: 0

Related Questions