Take the 2-minute tour ×

I'm fooling around with Python a little bit, organizing my imports and stuff.

1st specific situation:

When I import a same module in two different scripts, the module isn't imported twice, right? First time, Python imports it, and second time, does he 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() #Does this return 10 or 5?

The question is in the last comment.

share|improve this question
What happened when you ran the code? –  Wooble May 8 '12 at 15:34
Yeah, I forgot to do that... But what about the first part of my question? –  jco May 8 '12 at 15:43
@Bane: I've updated my answer. –  ThiefMaster May 8 '12 at 15:53
You may find these interesting: lucumr.pocoo.org/2011/9/21/python-import-blackbox and lucumr.pocoo.org/2009/7/24/…. They both treat imports in Python. –  rubik May 8 '12 at 16:00

2 Answers 2

up vote 19 down vote accepted

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:

share|improve this answer

To answer your first question:

No, python does not get 'imported' twice. When python loads a module, it checks for the module in sys.modules. If it is not in there, it is put in there, and loaded.

To answer your second question:

Modules can define what names they will export to a from camelot import * scenario, and the behavior is to create names for the existing values, not to reference existing variables (python does not have references).

On a somewhat related topic, doing a from camelot import * is not the same as a regular import.

share|improve this answer
Looks like ThiefMaster beat me to answering the edit. I'm leaving this here just to keep the "from camelot import * is not the same as a regular import." –  Darthfett May 8 '12 at 15:56

Your Answer


