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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:43:28+00:00 2026-05-28T20:43:28+00:00

How can I import a file that is in a parent directory within a

  • 0

How can I import a file that is in a parent directory within a python package (that is not on the path) into a file in a child dir?

I’m not totally clear on the vocabulary of python packaging so by way of example:

dir1/
    __init__.py
    runner.py
    in_dir1.py
    dir2/
        __init__.py
        in_dir2.py

dir1/in_dir1.py:

def example():
    print "Hello from dir1/in_dir1.example()"

dir1/dir2/in_dir2.py

import in_dir1   #or whatever this should be to make this work
print "Inside in_dir2.py, calling in_dir1.example()"
print in_dir1.example()

Given that dir1 is not on the python path I’m looking for the best way to import in_dir1 into in_dir2.

I tried from .. import in_dir1 and from ..dir1 import in_dir1 based on this Q/A but neither works. What is the correct way of executing that intent? This Q/A seems to contain the answer; however, I’m not quite sure what to make of it / how to actually solve my problem using PEP 366

Both __init__.py files are empty and I am on v2.6.

I’m attempting to do this without using any of the path hacks that Google keeps turning up.

  • 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-28T20:43:29+00:00Added an answer on May 28, 2026 at 8:43 pm

    The answer is in the link you gave:

    Relative imports use a module’s __name__ attribute to determine that
    module’s position in the package hierarchy. If the module’s name does
    not contain any package information (e.g. it is set to ‘main‘)
    then relative imports are resolved as if the module were a top level
    module, regardless of where the module is actually located on the file
    system.

    You cannot do relative imports in __main__ scripts (i.e. if you directly run python in_dir2.py).

    To solve this, what PEP 366 allows you to do is set the global __package__:

    import dir1
    if __name__ == '__main__':
        __package__ = 'dir1.dir2'
        from .. import in_dir1
    

    Note that the package dir1 still has to be on sys.path! You can manipulate sys.path to achieve this. But by then, what have you achieved over absolute imports?

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

Sidebar

Related Questions

A user can import data into our website from a file. The data normally
Following this example, I can list all elements into a pdf file import pyPdf
Two questions: How can I import a file from a web address, without a
How can you import a foxpro DBF file in SQL Server?
How can I import a variable from an external file? What I want to
Using import aliasing in one file/class, we can reference class library namespaces by assigning
I can import MySQLdb from the interactive python command line but Zope gives me
To my surprise and delight I read that an adminsitrator can import (nearly directly)
I'm trying to create a class that can be added to a Layout file,
I have denormalized data (coming from a file) that needs to be imported into

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.