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 9247897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:51:04+00:00 2026-06-18T09:51:04+00:00

I have the following setup of a library in python: library | – __init__.py

  • 0

I have the following setup of a library in python:

library
  | - __init__.py
  | - lib1.py
  | - ...
  | - tools
        | - __init__.py
        | - testlib1.py

so in other words, the directory library contain python modules and the directory tools, which is a subdirectory of library contains e.g. one file testlib1.pyto test the library lib1.py.

testlib1.py therefore need to import lib1.py from the directory above to do some tests etc., just by calling python testlib1.py from somewhere on the computer, assuming the file is in the search path.
In addition, I only want ONE PYTHONPATH to be specified.

But we all know the following idea for testlib1.py does not work because the relative import does not work:

from .. import lib1
...
do something with lib1

I accept two kind of answers:

  1. An answer which describes how still to be possible to call testlib1.py directly as the executing python script.
  2. An answer that explains a better conceptual setup of of the modules etc, with the premise that everything has to be in the directory project and the tools has to be in a different directory than the actual libraries.

If something is not clear I suggest to ask and I will update the question.

  • 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-06-18T09:51:06+00:00Added an answer on June 18, 2026 at 9:51 am

    Actually, I found a solution, which does

    1. works with the current implementation
    2. does not require any changes in PYTHONPATH
    3. without the need to hardcode the top-lebel directory

    In testlib1.py the following code will do (has been tested):

    import os
    import sys
    dirparts = os.path.dirname(os.path.abspath(__file__)).split('/')
    sys.path.append('/'.join(dirparts[:-1]))
    
    import  mylib1
    

    Not exactly sure this is a very clean or straightforward solution, but it allows to import any module from the directory one level up, which is what I need (as the test code or extra code or whetever is always located one level below the actual module files).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following setup: GDAL library with Python bindings (SWIG) Some glue code
I have the following setup: ASP.net 3.5 Web Site Project C# Class Library with
I have the following setup of a Scala application and a common core library:
I have following setup, but when I put 1024 and replace all 512 with
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following setup: Table: Question QuestionId Table: QuestionTag QuestionId TagId Table: Tag
I have the following setup, and I need to know how to persist state.
I have the following setup, and the empty view text doesn't show up... protected
I have the following setup: eclipse a standard Java project (A) an eclipse plugin
I have this following setup, a textarea named with some data in it that

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.