Python Func Reload
# Python reload() Function
In Python, the `reload()` function is used to reload a previously imported module. This is highly useful during development when you have modified a module's source code and want to test the changes in an active, interactive Python session without restarting the entire interpreter.
---
## Introduction
When you import a module in Python, the interpreter executes the module's code and caches it in `sys.modules`. Subsequent import statements simply reference this cached version.
If you make changes to the module's source file on disk, running `import module_name` again will not load the updated code. To force Python to recompile and reload the module, you must use the `reload()` function.
### Version Compatibility and Evolution
The location of the `reload()` function has changed across different Python versions:
* **Python 2.x**: `reload()` is a built-in function and can be called directly.
* **Python 2.x to Python 3.3**: The function was moved to the `imp` standard library module.
* **Python 3.4 and later**: The `imp` module was deprecated, and `reload()` was moved to the `importlib` standard library module.
---
## Syntax and Usage
Depending on your Python version, you must import and call `reload()` using the appropriate syntax:
### Python 2.x (Built-in)
```python
reload(module)
```
### Python 2.x to Python 3.3 (`imp` module)
```python
import imp
imp.reload(module)
# Or:
from imp import reload
reload(module)
```
### Python 3.4 and Later (`importlib` module)
```python
import importlib
importlib.reload(module)
# Or:
from importlib import reload
reload(module)
```
### Parameters
* **`module`**: The module object that you want to reload. This must be a module object that has already been successfully imported; it cannot be a string containing the module name.
### Return Value
* Returns the **module object** itself (the reloaded version).
---
## Code Examples
### 1. Basic Usage in Python 3.4+
Suppose you have a local module named `my_config.py` with the following content:
```python
# my_config.py
version = "1.0"
```
You import it in an interactive session:
```python
>>> import my_config
>>> my_config.version
'1.0'
```
Now, you modify `my_config.py` on your disk to change the version to `"2.0"`. To apply this change without restarting your Python session:
```python
>>> import importlib
>>> importlib.reload(my_config)
>>> my_config.version
'2.0'
```
---
### 2. Reloading the `sys` Module (Python 2.x)
In Python 2.x, `reload()` was frequently used on the `sys` module to reset the default system encoding to UTF-8:
```python
>>> import sys
>>> sys.getdefaultencoding() # Check current default encoding
'ascii'
>>> reload(sys) # Reload sys to expose setdefaultencoding
>>> sys.setdefaultencoding('utf8') # Set new default encoding
>>> sys.getdefaultencoding()
'utf8'
```
---
### 3. Reloading the `sys` Module (Python 3.4+)
In modern Python versions, you can reload built-in modules like `sys` using `importlib`:
```python
>>> import sys
>>> import importlib
>>> importlib.reload(sys)
```
---
## Important Considerations
While `reload()` is incredibly useful for interactive development, you should be aware of its limitations and behaviors:
1. **In-Place Updates**: `reload()` re-executes the module's code in the same module namespace. Existing objects in the module are overwritten if they are redefined, but old objects that were removed from the source code are **not** deleted.
2. **Reference Retention**: If other modules or namespaces imported objects from the reloaded module using `from module import object`, those references **will not** be updated to point to the new objects. They will still point to the old objects created during the initial import.
3. **Class Instances**: Existing instances of classes defined in the reloaded module are not updated. They will continue to use the old class definitions and methods. Only new instances created after the reload will use the updated class definitions.
4. **Built-in Modules**: Some built-in or dynamically loaded C modules (like `sys` or `marshal`) might not be fully reinitialized upon reload, though the module dictionary is refreshed.
YouTip