Importing packages into tests with python

0

I have a project with the following structure:

__init__.py
setup.py
- convert_keys/
    - __init__.py
    - convert.py
- tests/
    - test_convert_keys.py

Within my tests file I tried to import as follows;

from convert_keys import convert

but when trying to run the test file the error below appears;

python tests/test_convert_keys.py

  

Traceback (most recent call last):     File "tests / test_convert_keys.py", line 3, in       from convert_keys import convert   ImportError: No module named convert_keys

My test file has only one class that inherits unittest.TestCase , what is missing so that the file inside tests can import the file in convert_keys ?

    
asked by anonymous 16.06.2018 / 22:15

1 answer

0

As I do not know how the structure of your project is, the simplest solution is to put its directory in path of Python . You can do it in two ways. The first is by environment variable PYTHONPATH :

export PYTHONPATH=$( pwd )
python tests/test_convert_keys.py

The other, altering your code, would add it to the path of the system directly from within Python .

import sys
sys.path.append('/tmp/project') # no caso o diretório do teu projeto
from convert_keys import convert
    
17.06.2018 / 05:21