1

I have recently started working on a project and have been tasked with implementing some new features as well as unit testing for those features. I have been trying to import modules into the unit testing file but when I run it I come across an ImportError: No module named Developing.algorithms when I try to import into test_algorithms.py

I have tried importing with both

from Developing import algorithms as algo

and

import Developing.algorithms as algo

My structure is similar to this Testing project that I made:

Testing/
    __init__.py
    Developing/
        __init__.py
        algorithms.py
    Master (Stable)/
    Tests/
        __init__.py
        test_algorithms.py

And I run into:

ImportError: No module named Developing.algorithms

Or when I change the import to: from Developing import algorithms

ImportError: No module named Developing

I have read many similar questions and from those I have learned to include init.py files into each directory that has a file that I want to import. I currently do not have any errors according to PyCharm but when I run it from terminal I run into that import error. I also do not want to modify the system / python path as I read that everyone that uses the project would have to so the same. So how can I import from parallel directories without changing paths?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
user3591079
  • 283
  • 1
  • 2
  • 10
  • 2
    Where are you importing this from? Where in your directory structure would that script in which you are importing `Developing.algorithms` lie? – Anand S Kumar Jul 06 '15 at 14:32
  • I am trying to import into test_algorithms.py – user3591079 Jul 06 '15 at 14:34
  • What do your `import` statements look like? Something like `import Developing.algorithms` or `import Testing.Developing.algorithms`? Sometimes you have to write the entire path name. – Steven Correia Jul 06 '15 at 14:34
  • I have tried both: "from Developing import algorithms as algo" and "import Developing.algorithms as algo." When I use Testing.Developing.algorthms I run into a "Unresolved reference" in PyCharm despite having the __init__ files – user3591079 Jul 06 '15 at 14:36
  • possible duplicate of [Attempted relative import in non-package even with \_\_init\_\_.py](http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py) – bruno desthuilliers Jul 06 '15 at 14:50

1 Answers1

3

You will need to add the directory Testing into your PYTHONPATH env variable, to be able to import Developing.algorithms directly (or the directory above Testing to be able to import Testing.Developing.algorithms ).

In windows, you can set the PYTHONPATH variable as -

set PYTHONPATH=\path\to\Testing\;%PYTHONPATH%

In Bash , you can try -

export PYTHONPATH=/path/to/testing/:$PYTHONPATH

Programatically (from python) , you can do the following before you try to import Developing.algorithms -

import sys
sys.path.append('/path/to/Testing/')
from Developing import algorithms # or how ever you want to import.

Also, you do not need to do all of the above, any one would do - either setting PYTHONPATH env variable, or using sys.path .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • This seems to work, however; won't the path be different for every computer? I will look into using os to over come this. – user3591079 Jul 06 '15 at 14:50
  • The correct solution to setting the path is to always use a virtualenv. – Daniel Roseman Jul 06 '15 at 14:51
  • @user3591079 Would you be testing this on multiple computers? If so, I think you should consider restructuring your directory structure such that it is possible to import the files without setting anything in `PYTHONPATH` . Or you can use `sys.path.append` with relative path to `Testing` folder. – Anand S Kumar Jul 06 '15 at 14:58
  • @AnandSKumar Yes this will be tested on multiple computers, which is why I am hesitant to use the PYTHONPATH method. I will look into sys.path.append using the relative path. I am also a new member on this project so I am hesitant to recommend restructuring the project. In that case, would it be recommended to put the Testing folder within the folder that has files to test? For instance, /Developing/Tests? – user3591079 Jul 06 '15 at 15:02
  • @user3591079 I would say it would be recomend putting the testing script side by side (on the same folder) as the scripts that they would be testing (that is `Developing` , I hope) – Anand S Kumar Jul 06 '15 at 15:04