Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to load a module twice?

Is there a way to load a module twice in the same python session?
To fill this question with an example: Here is a module:

Mod.py

x = 0

Now I would like to import that module twice, like creating two instances of a class to have actually two copies of x.

To already answer the questions in the comments, "why anyone would want to do that if they could just create a class with x as a variable":
You are correct, but there exists some huge amount of source that would have to be rewritten, and loading a module twice would be a quick fix^^.

like image 968
Woltan Avatar asked Jun 28 '11 14:06

Woltan


People also ask

Can we import a module twice?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

Can you import same module twice in Python?

In fact, Python only loads the module when it is imported in the first file and all subsequent files simply set the name to refer to the already loaded module. While it is possible to override this behavior so that each file has its own copy of the module, it is generally not recommended.

What happens when you import a module Python?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

What happens when a package is imported?

If no file that fits the module is found, an ImportError will be raised. An empty module object is created, and the code in the module is executed with the module's __dict__ as the execution namespace. The module object is placed in sys. modules , and injected into the importer's namespace.


2 Answers

Yes, you can load a module twice:

import mod
import sys
del sys.modules["mod"]
import mod as mod2

Now, mod and mod2 are two instances of the same module.

That said, I doubt this is ever useful. Use classes instead -- eventually it will be less work.

Edit: In Python 2.x, you can also use the following code to "manually" import a module:

import imp

def my_import(name):
    file, pathname, description = imp.find_module(name)
    code = compile(file.read(), pathname, "exec", dont_inherit=True)
    file.close()
    module = imp.new_module(name)
    exec code in module.__dict__
    return module

This solution might be more flexible than the first one. You no longer have to "fight" the import mechanism since you are (partly) rolling your own one. (Note that this implementation doesn't set the __file__, __path__ and __package__ attributes of the module -- if these are needed, just add code to set them.)

like image 72
Sven Marnach Avatar answered Sep 20 '22 05:09

Sven Marnach


Deleting an entry from sys.modules will not necessarily work (e.g. it will fail when importing recurly twice, if you want to work with multiple recurly accounts in the same app etc.)

Another way to accomplish this is:

>>> import importlib
>>> spec = importlib.util.find_spec(module_name)
>>> instance_one = importlib.util.module_from_spec(spec)
>>> instance_two = importlib.util.module_from_spec(spec)
>>> instance_one == instance_two
False
like image 38
Janis Kirsteins Avatar answered Sep 18 '22 05:09

Janis Kirsteins