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?

3
  • 1
    from what I understood you can mock your configurations module for tests Commented Aug 13, 2019 at 10:32
  • Is it not possible to do import test1 as t1 and import test1 as t2? Commented Aug 13, 2019 at 10:35
  • @Ardweaden Due to the nature of python if a module is loaded once, if you load it again you get the reference to the first one, so no that is not possible Commented Aug 13, 2019 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.