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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:52:14+00:00 2026-06-08T08:52:14+00:00

I just noticed that relative import like this: from .foo import myfunc print myfunc

  • 0

I just noticed that relative import like this:

from .foo import myfunc
print myfunc  # ok
print foo  # ok

imports both foo and myfunc. Is such behaviour documented anywhere? Can I disable it?

— Update

Basically problem is following.

bar/foo/__init__.py:

__all__ = ['myfunc']

def myfunc(): pass

bar/__init__.py:

from .foo import *
# here I expect that there is only myfunc defined

main.py:

import foo
from bar import *  # this import shadows original foo

I can add __all__ to the bar/__init__.py as well, but that way I have to repeat names in several places.

  • 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-08T08:52:15+00:00Added an answer on June 8, 2026 at 8:52 am

    I am assuming your package layout is

    my_package/
        __init__.py
            from .foo import myfunc
        foo.py
            def myfunc(): pass
    

    The statement from .foo import myfunc first imports the module foo, generally without introducing any names into the local scope. After this first step, myfunc is imported into the local namespace.

    In this particular case, however, the first step also imports the module into the local namespace: sub-modules of packages are put in the package’s namespace upon importing, regardless from where they are imported. Since __init__.py is also executed in the package’s namespace, this happens to conincide with the local namespace.

    You cannot reasonably disable this behaviour. If you don’t want the name foo in your package’s namespace, my advice is to rename the module to _foo to mark it as internal.

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

Sidebar

Related Questions

I've just noticed that if I do a MySQL request like this one: SELECT
I just noticed that I could use an a variable as an argument, like
I just noticed that the email sent from my host using mail() function also
I just noticed that by instance famous application like chrome didn't have any 64
I just noticed that Chromium was installed in AppData in both Vista and XP.
I just noticed that you can do this in C#: Unit myUnit = 5;
I just noticed that moving the mouse cursor across a tooltip does not seem
I just noticed that java.beans.Introspector getBeanInfo does not pickup any superinterface's properties. Example: public
I just noticed that say http://s7.addthis.com/js/250/addthis_widget.js#pub=PUBID does the equivalent of http://s7.addthis.com/js/250/addthis_widget.js?pub=fct-250 but is much
I just noticed that for vector push_back it is push back a reference to

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.