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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:44:11+00:00 2026-06-06T18:44:11+00:00

Python allows aliasing of imports, through …as <ALIAS> clauses in the import statement, like

  • 0

Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like this:

import mymodule as somealias
from myothermodule import spam as spamalias, ham as hamalias

Now, in the default case at least, import statements, including those that have as-clauses like the ones above, result in calls to __builtin__.__import__ whose arguments lists do not include the names of such aliases. IOW, AFAICT, the following “aliasing-free” import statements produce identical calls to __builtin__.__import__ as do the import statements quoted above:

import mymodule
from myothermodule import spam, ham

So how do these alias names enter the import process? And does Python provide ways to customize how alias names are used by the import process?

(I don’t have any specific application in mind; I’m just trying to better understand how Python works.)

  • 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-06T18:44:15+00:00Added an answer on June 6, 2026 at 6:44 pm

    The function __import__() does not bind any names in the calling scope. Basically,

    import foo
    

    is similar to

    foo = __import__("foo")
    

    and

    import foo as bar
    

    is similar to

    bar = __import__("foo")
    

    Name binding happens in the calling scope, not inside the function, so __import__() does not need to know what name the module will finally be bound to.

    This can also be seen from the CPython byte code of these statements:

    >>> dis.dis(compile("import foo", "", "exec"))
      1           0 LOAD_CONST               0 (-1)
                  3 LOAD_CONST               1 (None)
                  6 IMPORT_NAME              0 (foo)
                  9 STORE_NAME               0 (foo)
                 12 LOAD_CONST               1 (None)
                 15 RETURN_VALUE        
    >>> dis.dis(compile("import foo as bar", "", "exec"))
      1           0 LOAD_CONST               0 (-1)
                  3 LOAD_CONST               1 (None)
                  6 IMPORT_NAME              0 (foo)
                  9 STORE_NAME               1 (bar)
                 12 LOAD_CONST               1 (None)
                 15 RETURN_VALUE        
    

    The IMPORT_NAME opcode implicitly calls __import__() and leaves the resulting module on the internal stack. The subsequent STORE_NAME pops the module from the stack and binds it to a local name. You can see the only difference between the two snippets is which name the module gets stored to.

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

Sidebar

Related Questions

The yield statement in python allows simple iteration from a procedure, and it also
easy_install python extension allows to install python eggs from console like: easy_install py2app But
Python's setdefault allows you to get a value from a dictionary, but if the
Is there a library for C# that allows similar functionality to python's struct from
I have the following python function that allows me to run shell commands from
I'm trying to port an application from C# to Python. The application allows the
Hi guys I've fonund this useful python script that allows me to get some
Python allows conversions from string to integer using any base in the range [2,36]
Python allows easy creation of an integer from a string of a given base
I'm building a website which allows you to write python code online just like

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.