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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:32:23+00:00 2026-05-25T00:32:23+00:00

I noticed I’ve made about 5 questions asking about relative imports, each with working

  • 0

I noticed I’ve made about 5 questions asking about relative imports, each with working solutions but with different situations. The more I read the docs and the more I try out different cases the more I’m getting confused and questioning whether I’m actually reading the words correctly.

From pep 328:

A single leading dot indicates a relative import,
starting with the current package. Two or more leading dots
give a relative import to the parent(s) of the current package,
one level per dot after the first.

My understanding is that “current package” is the keyword here. What IS the “current package”? Is it the package of the module where execution begins? (ie: the file with __ name __ == “__ main __”).

Anyways, here is a simple situation of me using .. notation.

main/
  lib/
    __init__.py
    myLib.py
  plugin/
    __init__.py
    needLib.py
  run.py

run.py imports needLib: from plugin import needLib

needLib imports myLib: from ..lib import myLib

What’s going through my head: (needLib) goes up a package, go into lib, grab myLib

Here is how I execute: python run.py

Result: Attempted relative import beyond toplevel package

But when I changed the import call to from .lib import myLib, it works.

I don’t understand why the latter works and the .. notation doesn’t. Of course, main isn’t a package, so how about I just throw in a init.py there as well, resulting in

main/
  lib/
    __init__.py
    myLib.py
  other/
    __init__.py
    needLib.py
  __init__.py
  run.py

But that didn’t make a difference; still attempting to import beyond top-level package

Check out this question: python: forcing relative imports to search from script file

The .. notation actually worked! Now that I think about it, I don’t understand why it works. Execution in that scenario started 2 levels up from where the relative import takes place, while execution in this scenario starts 1 level up from where the relative import takes place. Other than where I begin execution, the situation is identical.

Both are me saying “go up one directory, go into the lib package, and grab the module you want”.

What is the key difference between this situation and the referenced situation? Why does .. work there, but not here? Does me using 2.6 have anything to do with this? Or perhaps how I am executing it? python run.py

  • 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-25T00:32:24+00:00Added an answer on May 25, 2026 at 12:32 am

    As you found out, from ..lib import myLib does not work as main is not a package. Relative imports only work within a module’s own package. In your example, lib and other are two distinct packages.

    Putting an __init__.py in main only works if you move run outside of the newly created main package, so that the current directory (part of sys.path) is not within a package.

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

Sidebar

Related Questions

I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I noticed that when I do something like this (with jQuery, but I don't
Noticed an interesting GUI based setting in MacVim, that seems useful but I can't
I noticed that tail +2 is supported in Solaris ksh, but in Red Hat
I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering
I noticed django use this to process the string, but can I get a
Noticed this today when a patch was submitted with the following line: lblCompletionTime.Text =
I noticed, what CALayer, on iOS SDK haven't autoresizingMask property. I'm not sure what
I noticed when doing a query with multiple JOINs that my query didn't work
I noticed while playing around with Chrome that both of these functions work the

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.