21

I have checked as many SO pages as I could and tried everything I found, but none have been successful. I also checked the PEP page regarding importing and tried every example, none of which worked.

I have a tests folder with unit tests in them, and I need to import the module I want to test. The modules are in a folder called 'src' which is next to the src folder.

The folders/files look something like this:

Project / 
        src /
            stringbuilder.py
            __init__.py
        tests / 
            stringbuilder_test.py
            __init__.py
        main.py
        __init__.py

I have tried everything I could: adding __init__.py to every folder making it a module including the project's main folder.

import src.module_to_test 
from ..src.module_to_test import function_to_test
from ..src import module_to_test

I have tested all other combinations all of which have failed. I am starting to think there must be something wrong with either my settings or understanding-- I thought this was suppose to be simple.

If I am making any obvious errors please let me know.

from tests import stringbuilder

Error Message:

$ ./stringbuilder_test.py
Traceback (most recent call last):
  File "./stringbuilder_test.py", line 14, in <module>
    from tests import stringbuilder
ImportError: No module named tests

The same error occurs for (but tests = src):

from src import stringbuilder
13
  • do you have a __init__.py file in that folder?
    – piggy_Yu
    Nov 4, 2011 at 17:10
  • Yes, there is one in the project folder, src folder and tests folder.
    – anon
    Nov 4, 2011 at 17:11
  • could you paste the error msg? maybe it's useful.
    – piggy_Yu
    Nov 4, 2011 at 17:15
  • try: from stringbuilder_test import class(which class you need to import)
    – piggy_Yu
    Nov 4, 2011 at 17:20
  • "src" or "tests"? Can you give us more details about your files and folders hierarchy?
    – Nicolas
    Nov 4, 2011 at 17:21

3 Answers 3

12

If scripts are executed from inside a package, then various hacks need to be employed to get the imports to work properly. Probably the most common of these is to manipulate sys.path:

import sys, os

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

from src import stringbuilder

del sys.path[0]

There is no way to achieve this using normal import statements.

A generally better solution is to simply avoid running scripts inside packages altogether. Instead, put all scripts outside the package in a containing directory. Given that the directory of the currently running script is automatically added to the start of sys.path, this will guarantee that the package can be always be directly imported, no matter where the script is executed from.

So the directory structure might look something like this:

project /
    package /
        __init__.py
        src /
            __init__.py
            stringbuilder.py
        tests / 
            __init__.py
            stringbuilder_test.py
    main.py
    test.py

The test.py script can then import its tests like this:

from package.tests import stringbuilder_test

And stringbuilder_test.py can import the src modules like this:

from package.src import stringbuilder
1
from folder import my_module

If there is a __init__.py file in folder.

2
  • This failed, it said there's no module named folder.
    – user427390
    Nov 4, 2011 at 17:12
  • Er, I think you were meant to replace folder with name of your folder! Nov 4, 2011 at 17:13
-9

Add that folder to your PATH.

1
  • Adding it to PYTHONPATH is slightly less dreadful but misses the point of the question. Adding to PATH won't help in the slightest.
    – Wooble
    Nov 4, 2011 at 17:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.