简体   繁体   中英

Using code in init.py when running python from command line

I'm setting up some code for unittesting. My directory currently looks like this:

project/
    src/
        __init__.py
        sources.py
    test/
        __init__.py
        sources_test.py

In __init__.py for the test directory, I have these two lines:

import sys
sys.path.insert(0, '../')

In the test files, I have the line import src.sources .

When I use nose to run these tests from the project directory, everything works just fine. If I try to run the tests individually it gives me this error:

ImportError: No module named src.sources

I assume that this is because when I run the test from the command line it isn't using __init__.py. Is there a way I can make sure that it will use those lines even when I try to run the tests individually?

I could take the lines out of __init__.py and put them into my test files, but I'm trying to avoid doing that.

To run the tests individually I am running python sources_test.py

You're really trying to abuse packages here, and that isn't a good idea.

The simple solution is to not run the tests from within the tests directory. Just cd up a level, then do python tests/sources_test.py .

Of course that in itself isn't going to import test/__init__.py . For that, you really need to import the package. So python -m tests.sources_test is probably a better idea… except, of course, that if your package is made to be run as a script but not to be imported, that won't work.

Alternatively, you could (on POSIX platforms, at least) do PYTHONPATH=.. python sources_test.py from within tests . This is a bit hacky, but it should work.

Or, better, combine the above, and, from outside of tests , do PYTHONPATH=. python tests/sources_test.py PYTHONPATH=. python tests/sources_test.py .

A really hacky workaround is to explicitly import __init__ . This should basically work for you simple use case, but everything ends up wrong—in particular, you end up with a module named __init__ instead of one named test , and of course your main module isn't named test.sources_test , and in fact there is no test package at all. Unless you accidentally re-import anything after modifying sys.path , in which case you may get duplicates of the modules.

If you write

import src.source

the python interpreter looks into the src directory for a __init__.py file. If it exists, you can use the directory as a package name. If your are not in your project directory, which is the case when you are in the src directory, then python looks into the directories in $PYTHONPATH environment variable (at least in linux, windows should also have some environment variable, maybe with another name), if it can find some directory src with a __init__.py file in it. Did you set your $PYTHONPATH ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM