Take the 2-minute tour ×

I've the following source code structure


where testmsg defines the following constant:

MSG = "Test message"

and sub/testprinter.py:

import testmsg

print("The message is: {0}".format(testmsg.MSG))

But I'm getting ImportError: No module named testmsg

Shouldn't it be working since the package structure? I don't really want to extend sys.path in each submodule and I don't even want to use relative import.

What am I doing wrong here?

share|improve this question
How you do execute the code? –  Simeon Visser Jul 9 '12 at 10:46
@SimeonVisser running python sub/testprinter.py, but also python testprinter.py within sub directory doesn't work. –  user1543863 Jul 9 '12 at 12:43
add comment

3 Answers

up vote 2 down vote accepted

It all depends on which script you run. That script's path will be added to python's search path automatically.

Make it the following structure:


Then run TestApp/run_test.py first:

from testapp.sub.testprinter import functest ; functest()

Then TestApp/testapp/sub/testprinter.py could do:

from testapp.testmsg import MSG

More good hints here;

share|improve this answer
may it depend on the fact that I didn't generate a setup.py file? What I mean is that I followed the python package structure but I didn't generate installation method, assuming it could also work without having to setup.py install it. –  user1543863 Jul 9 '12 at 12:44
@CodeShining, no it doesn't matter. I only included the setup.py in the example for clarity. What really matters, as I said in the first paragraph of the answer, is which file you're running in first place. If you start (run) a file inside the package, it won't see the package. So it's a good practice to have your main script (the one you run) outside the package. –  nosklo Jul 9 '12 at 18:30
add comment

Use relative import like below

from .. import testmsg
share|improve this answer
but being a package shouldn't it be working as expected? I wouldn't use relative if it's expected to work –  user1543863 Jul 9 '12 at 10:49
CodeShining, look at Dido's decision for python 3.5 PEP-328 for standard. link –  Vinayak Kolagi Jul 9 '12 at 11:02
add comment

This question has the answer - dynamic importing:

How to import a python file in a parent directory

import sys
import parent.file1

Here's something I made to import anything. Of course, you have to still copy this script around to local directories, import it, and use the path you want.

import sys
import os

# a function that can be used to import a python module from anywhere - even parent directories
def use(path):
    scriptDirectory = os.path.dirname(sys.argv[0])  # this is necessary to allow drag and drop (over the script) to work
    importPath = os.path.dirname(path)
    importModule = os.path.basename(path)
    sys.path.append(scriptDirectory+"\\"+importPath)        # Effing mess you have to go through to get python to import from a parent directory

    module = __import__(importModule)
    for attr in dir(module):
        if not attr.startswith('_'):
            __builtins__[attr] = getattr(module, attr)
share|improve this answer
add comment

Your Answer

