Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to reload modules that have been imported with *

Tags:

python

I know that if I import a module by name import(moduleName), then I can reload it with reload(moduleName)

But, I am importing a bunch of modules with a Kleene star:

from proj import *

How can I reload them in this case?

like image 238
Anas Elghafari Avatar asked Jul 21 '14 11:07

Anas Elghafari


People also ask

How do I rerun a Python module?

The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have made changes to the code.

What is import * in Python?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.

How do I reload a Python 3 module?

If you truly must reload a module in Python 3, you should use either: importlib. reload for Python 3.4 and above.


1 Answers

I think there's a way to reload all python modules. The code for Python 2.7 is listed below: Instead of importing the math module with an asterisk, you can import whatever you need.

from math import *
from sys import *

Alfa = modules.keys()
modules.clear()

for elem in Alfa:
    str = 'from '+elem+' import *'
    try:     
        exec(str)     
    except: 
        pass
like image 52
Pablo Stark Avatar answered Sep 25 '22 20:09

Pablo Stark