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

  • Home
  • SEARCH
  • 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 5849949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:04:21+00:00 2026-05-22T13:04:21+00:00

Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__

  • 0

Take the following code example:

File package1/__init__.py:

from moduleB import foo
print moduleB.__name__

File package1/moduleB.py:

def foo(): pass

Then from the current directory:

>>> import package1
package1.moduleB

This code works in CPython. What surprises me about it is that the from ... import in __init__.py statement makes the moduleB name visible. According to Python documentation, this should not be the case:

The from form does not bind the module name

Could someone please explain why CPython works that way? Is there any documentation describing this in detail?

  • 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-22T13:04:22+00:00Added an answer on May 22, 2026 at 1:04 pm

    The documentation misled you as it is written to describe the more common case of importing a module from outside of the parent package containing it.

    For example, using “from example import submodule” in my own code, where “example” is some third party library completely unconnected to my own code, does not bind the name “example”. It does still import both the example/__init__.py and example/submodule.py modules, create two module objects, and assign example.submodule to the second module object.

    But, “from..import” of names from a submodule must set the submodule attribute on the parent package object. Consider if it didn’t:

    1. package/__init__.py executes when package is imported.

    2. That __init__ does “from submodule import name”.

    3. At some point later, other completely different code does “import package.submodule”.

    At step 3, either sys.modules[“package.submodule”] doesn’t exist, in which case loading it again will give you two different module objects in different scopes; or sys.modules[“package.submodule”] will exist but “submodule” won’t be an attribute of the parent package object (sys.modules[“package”]), and “import package.submodule” will do nothing. However, if it does nothing, the code using the import cannot access submodule as an attribute of package!


    Theoretically, how importing a submodule works could be changed if the rest of the import machinery was changed to match.

    If you just need to know what importing a submodule S from package P will do, then in a nutshell:

    1. Ensure P is imported, or import it otherwise. (This step recurses to handle “import A.B.C.D”.)
    2. Execute S.py to get a module object. (Skipping details of .pyc files, etc.)
    3. Store module object in sys.modules[“P.S”].
    4. setattr(sys.modules["P"], "S", sys.modules["P.S"])
    5. If that import was of the form “import P.S”, bind “P” in local scope.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following code in C/C++, for example: int foo[] = {0, 0, 0,
Take this following code from an example HR system. The user has the ability
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Take for example the following code: try { Response.Redirect(someurl); } finally { // Will
Take the following code snippit: f = open(/mnt/remoteserver/bar/foo.bin, O_RDONNLY); while (true) { byteseread =
Take the following two lines of code: for (int i = 0; i <
I'm trying to write a piece of code that will do the following: Take
Take the following string as an example: The quick brown fox Right now the
Take the following class as an example: class Sometype { int someValue; public Sometype(int

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.