0

I am trying to have two instances of the same python module.

I have looked at this, and the second answer seems promising. But if I try to call methods on the generated modules (with importlib) it tells me the methods are not there. And if I look at help(module) the name is correct, but the methods are not listed

The accepted solution in the link above didnt work for me aswell. The module was loaded, but I got a "KeyError" when trying to delete it. (And I dont like the idea of doing that)

I have a local configuration file that I would like to change in order to test some functionality, therefore I need to import the same module twice. The basic idea is p2p in a network, and the p2p is implemented in a Module. So I need to import it twice in order to have different ports to connect to etc. This will not be used in production, this is just for testing.

My setup is basically like this:

test_module.py

from config import local_config
    def print_config():
        print(local_config)

test.py

import config
config.local_config = "test1"
import test_module as test1
reload(config)
config.local_config = "test2"
import test_module as test2

def test_module():
    print(test1.print_config())
    print(test2.print_config())

test_module()

Is there a way to do that? Because as far as I know python imports modules as Objects? Right?

Matthias
  • 12,873
  • 6
  • 42
  • 48
SimonSchuler
  • 155
  • 8

0 Answers0