Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6792291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:57:03+00:00 2026-05-26T17:57:03+00:00

I have checked as many SO pages as I could and tried everything I

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:57:04+00:00Added an answer on May 26, 2026 at 5:57 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have checked many questions here but none has my answer. I appreciate if
I have already checked many questions in StackOverflow but none of them worked for
I have a page with many forms on it. could be 1..200. None of
We have many projects with several files inside each. Files can be checked in
I have checked this similar question, but the suggestions did not solve my problem:
I have checked few other posts and found some UIAnimation transitions that kind-of give
I have checked IE8, FF 3.5 , Opera 9 none of them have support
I have checked the source codes of ASIHTTPRequest and could not make sure which
Apparently many people have run into this problem, but I have yet to be
I have checked many questions and answers regarding how to stop any browser at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.