Problem with relative import and nosetests

Hi there, I can’t seem to figure this out. I am trying to run nosetests on a project. This project imports from another file in the same directory. When I run the file directly, nothing goes wrong. When I run my tests with nosetests, I get an error concerning the import statement (that is in the module, not in the test file). It seems to have something to do with the tests being in another directory than the modules, and nosetests trying to import the file from the tests file path.

My directory structure is:

            (a bunch of txt files)

In ‘’ I use ‘import lexicon’. This works fine when running
However, when running nosetests this gives an import error:

File “/home/pi/projects/textgame/bureaucratica/”, line 14, in
import lexicon
ModuleNotFoundError: No module named ‘lexicon’

When I change it to ‘from bureaucratica import lexicon’ nosetests works.
But then, running itself gives an import error.

Is there a way to make both work?


At the risk of sounding flippant, run a search on this forum about nose and you’ll soon see numerous similar issues.

The general solution is to ditch nose in favour of Pytest or the built in unittest. Nose is deprecated (see from nose site):

Note to Users

Nose has been in maintenance mode for the past several years and will likely cease without a new person/team to take over maintainership. New projects should consider using Nose2, py.test, or just plain unittest/unittest2.

Thank you.

I wanted to try and finish the book with nosetests before switchting to unittest. But this annoying error is enough motivation to start with that now. I’ll have another look at pytest too.

I know what you mean @Tim - perhaps use Unittest first as syntax is very similar, whereas pytest, which is much better has a different syntax (just assert).

I’m trying pytest now, in a separate virtualenv. I do like the shorter syntax. The same import error came up again though, I fixed it by adding the module’s directory to pythonpath. I’m reading here and there that this isn’t the right way to go, but since I’m working from a virtualenv I don’t see a big problem there for now.

1 Like

So, if you’re in textgame/ and you write this code:

import lexicon

Then, it will look for a file named textgame/ I believe you have to change this to:

import lexicon from bureaucratica

You might also have to force Python to look right there for files. On OSX you do this:


On Windows it should be:


Basically, where your terminal session is sitting determines what file is imported, so if you’re in textgame (and you should always be there forever, never cd to a lower directory) then it should work.

1 Like

Thanks for the suggestion!

I still did not get it fixed completely. I do understand what is going wrong now:

  • tries: “import lexicon” which works because it is in the same directory.

  • from textgame/ I run pytest. It imports which in turn imports but this time the import fails because we are not in the ‘bureaucratica’ directory.

  • Changing the import to “from bureaucratica import lexicon” in makes pytest work, but now running directly gives an error: there is no bureaucratica folder (because is already inside it).

Here is the package file structure again:

                (a bunch of txt files)

Great. Good news though, I finally found a fix that doesn’t require changing pythonpath from the terminal.

I found this on

Inside the bureaucratica folder I added a file called ‘’ with the following code:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from bureaucratica import lexicon
from bureaucratica import parser_tim

Then, I changed the imports in as follows:

from .context import lexicon
from .context import parser_tim

Now both pytest (running from textgame/) and (running from textgame/bureaucratica/) both work!

From what I understand, the file does the actual import and ensures that python searches from the root folder of the package. So the addition to pythonpath seems to be made from within the program itself now. This should make the imports more reliable on different installs as well. (Not that my package is ready to be thrown out there yet…)

Mystery solved?


Just using the dots to indicate the right folder works as well!


from . import lexicon
from . import parser_tim

No import problems in pytest or when running!