dynamic import of dynamically created modules failes

Pieter van Oostrum pieter-l at vanoostrum.org
Tue Mar 31 14:01:46 EDT 2020


"Dieter Maurer" <dieter at handshake.de> writes:

> Stephan Lukits wrote at 2020-3-31 17:44 +0300:
>>background:
>>
>>- a daemon creates package p1 (e.g. directory with __init__.py-file) and
>>in p1 a module m1 is created.
>>
>>- Then the daemon wants to import from m1, which functions (so far all
>>the time).
>>
>>- Then a module m2 is created in p1 and the daemon wants to import from
>>m2 which fails (most of the time but *not* always) with ModuleNotFoundError.
>>
>>See the little test script at the end which reproduces the problem.
>> ...
>
> I remember a similar report (some time ago). There, caching has
> been responsible. I have forgotten how to invalidate the caches.
> But, you could try logic found in
> `importlib._bootstrap_external.PathFinder.invalidate_caches`.

The first import creates a file __pycache__ in the directory p1.
To remove it use rmtree(path.join(P1,'__pycache__'))
Then the second import will succeed.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]


More information about the Python-list mailing list