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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:19:11+00:00 2026-06-03T08:19:11+00:00

I have two specific situations where I don’t understand how importing works in Python:

  • 0

I have two specific situations where I don’t understand how importing works in Python:

1st specific situation:

When I import the same module in two different Python scripts, the module isn’t imported twice, right? The first time Python encounters it, it is imported, and second time, does it check if the module has been imported, or does it make a copy?

2nd specific situation:

Consider the following module, called bla.py:

a = 10

And then, we have foo.py, a module which imports bla.py:

from bla import *

def Stuff ():
    return a

And after that, we have a script called bar.py, which gets executed by the user:

from foo import *
Stuff() #This should return 10 
a = 5
Stuff()

Here I don’t know: Does Stuff() return 10 or 5?

  • 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-03T08:19:12+00:00Added an answer on June 3, 2026 at 8:19 am

    Part 1

    The module is only loaded once, so there is no performance loss by importing it again. If you actually wanted it to be loaded/parsed again, you’d have to reload() the module.

    The first place checked is sys.modules, the cache of all modules that have been imported previously. [source]


    Part 2

    from foo import * imports a to the local scope. When assigning a value to a, it is replaced with the new value – but the original foo.a variable is not touched.

    So unless you import foo and modify foo.a, both calls will return the same value.

    For a mutable type such as a list or dict it would be different, modifying it would indeed affect the original variable – but assigning a new value to it would still not modify foo.whatever.

    If you want some more detailed information, have a look at http://docs.python.org/reference/executionmodel.html:

    The following constructs bind names: formal parameters to functions, import statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, for loop header, in the second position of an except clause header or after as in a with statement.

    The two bold sections are the relevant ones for you: First the name a is bound to the value of foo.a during the import. Then, when doing a = 5, the name a is bound to 5. Since modifying a list/dict does not cause any binding, those operations would modify the original one (b and foo.b are bound to the same object on which you operate). Assigning a new object to b would be a binding operation again and thus separate b from foo.b.

    It is also worth noting what exactly the import statement does:

    • import foo binds the module name to the module object in the current scope, so if you modify foo.whatever, you will work with the name in that module – any modifications/assignments will affect the variable in the module.
    • from foo import bar binds the given name(s) only (i.e. foo will remain unbound) to the element with the same name in foo – so operations on bar behave like explained earlier.
    • from foo import * behaves like the previous one, but it imports all global names which are not prefixed with an underscore. If the module defines __all__ only names inside this sequence are imported.

    Part 3 (which doesn’t even exist in your question :p)

    The python documentation is extremely good and usually verbose – you find answer on almost every possible language-related question in there. Here are some useful links:

    • http://docs.python.org/reference/datamodel.html (classes, properties, magic methods, etc.) ()
    • http://docs.python.org/reference/executionmodel.html (how variables work in python)
    • http://docs.python.org/reference/expressions.html
    • http://docs.python.org/reference/simple_stmts.html (statements such as import, yield)
    • http://docs.python.org/reference/compound_stmts.html (block statements such as for, try, with)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that two branches have diverged and a specific commit from one branch (and
I have two classes generated by LINQ2SQL both from the same table so they
Consider a situation. We have some specific C++ compiler, a specific set of compiler
I have the following situation: I have two threads thread1 , which is a
Let's imagine I have two threads which execute some database-oriented code in thread-specific TransactionScopes
Situation I have two .NET solutions ( Foo and Bar ) and a common
I have a specific situation where I've got an object that I want to
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two DropDownListBoxes one is called ddlDay and the other is ddlMonth. As

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.