Python offers two primary ways to incorporate code from other modules: import and from ... import .... Understanding their nuances, particularly regarding how they handle module objects in memory, is crucial for writing efficinet and predictable code.
Syntax and Memory Alllocation
The core difference lies in how Python loads and manages modules in memory.
-
from module import item: This statement specifically importsitem(which can be a function, class, or variable) frommodule. Python creates a separate copy ofitemin memory for the importing script. If another script imports the sameitemusingfrom ... import ..., it also gets its own distinct copy. Modifications toitemwithin one script do not affect other scripts or the original module because they operate on independent copies. -
import module: This statement imports the entiremodule. Python loads the module's attributes directly into memory. When multiple scripts import the same module usingimport module, they all reference a single, shared instance of that module in memory. Consequently, changes made to the module's attributes by one script will be visible to all other scripts that have imported the same module, including the original module itself.
Illustrative Examples
Let's consider a base module model_a.py:
# model_a.py
a = 3
b = 4
Scenario 1: Using from ... import ...
Script model1.py:
#!/usr/bin/python
from model_a import a, b
import model2
print(a)
print(b)
a = 5 # Modifying the local 'a'
print(a)
print(b)
print(model2.sum()) # Check if model1's modification affects model2
Script model2.py:
#!/usr/bin/python
import model_a
print('2', model_a.a)
print('2', model_a.b)
def sum():
return model_a.a + model_a.b
Output from running model2.py first, then model1.py:
2 3
2 4
3
4
5
4
7
As shown, modifying a in model1.py using from ... import ... does not affect the a referenced by model2.py.
Scenario 2: Using import ...
Script model1.py (modified to use import):
#!/usr/bin/python
import model_a
import model2
print(model_a.a)
print(model_a.b)
model_a.a = 5 # Modifying the module's attribute directly
print(model_a.a)
print(model_a.b)
print(model2.sum())
Output from running model2.py first, then the modified model1.py:
2 3
2 4
3
4
5
4
9
In this case, modifying model_a.a in model1.py directly impacts the value of a used by model2.py because both scripts share the same model_a instance in memory.
from ... import *
The from module import * syntax is a variation of from ... import ..., but it imports all public names from the module. It behaves similarly to other from ... import ... statements regarding memory, creating separate copies or references as appropriate for each imported name.
Summary of Differences
| Feature | import module |
from module import item |
|---|---|---|
| Memory | Shared module instance across all importers. | Separate copies of imported items for each importeer. |
| Modification | Modifications affect all importers and the original. | Modifications affect only the importer; others are isolated. |
| Usage | Access items via module.item. |
Access items directly by their name (item). |
Key Considerations:
- When multiple scripts need to access a module without modifying it, using
import modulecan be more memory-efficient due to the shared instance. - If one script needs to modify module attributes while others access them, the modifying script should use
from ... import ...to isolate its changes, or explicitly manage modifications to the shared instance if intended.