0

I need to use a modified version of a library modul containing a class. For my understanding the best practice for this is to create a local copy (with identical file- and classname) of said module in my own source folder, modify it and import the local version instead of the version from the library. So the folder structure is

/src/myscript.py
/src/module.py
/some/other/path/library/module.py

It works to import the local version of module in myscript.py with

from module import TheClass

It gets also executed. But for some reason later the class from the library module gets executed as well! So obviously there is a reimport happening in some of the other library code that im working with, that still refers to the library module.

How can I force python to always use the already imported local module.py, when a later import command elsewhere refers to the same module/classname?

Edit1: With myscript.py changed to

import sys
import importlib.util
spec = importlib.util.spec_from_file_location("module.TheClass", "module.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["library.subpack.module"] = foo
[...]

a later import DOES get redirected to the local module.py, but unfortunately this Codeline

[...]
from library.subpack.module import TheClass
[...]

then throws the error: ImportError: cannot import name 'TheClass' from 'module.TheClass' (module.py)

What module.py looks like (all versions):

import math
__all__ = ["TheClass"]
class TheClass(NestedObject):
[...]
  • You could try importing your local version with the method in [this answer](https://stackoverflow.com/a/67692/4245684). I don't think this will immediately prevent the reimport issue (if that does end up being the problem), but if you can find where the library module is reimported, and replace it with this explicit reference to your local module, that might get you there. – airdas Jun 16 '23 at 09:35
  • Thank you, this has an effect but please see Edit1 under my original Post... – anotherEmbodiment Jun 16 '23 at 11:25
  • `library.subpack.module` and `module` are two different fully qualified package names. `import module` puts an entry named `module` in `sys.modules`. `from library.subpack.module import ...` looks for entry named `library.subpack.module`. You can (I think) hack it by explicitly setting `sys.modules['library.subpack.module'] = sys.modules['module']`, but I think it would be better to avoid shadowing the module in the first place. (Or are you trying to intercept an import in a script/module that you can't modify?) – chepner Jun 16 '23 at 12:51
  • Does not work. Shadowing a module seemed pythonic to me, but I give it up now. How else can I implement modifications then? I can not change the original library. Everything my code needs to work must be in its own folder or accessed from officially released librarys. And I have to avoid code redundancies. – anotherEmbodiment Jun 16 '23 at 13:31

2 Answers2

0

try from .module import TheClass

0

After removing and reinstalling python completely and then reconfiguring my IDEs interpreter settings and run configurations, the local modules are imported as expected.