Search code examples
pythonimportanacondanamespace-package

Python how to remove path from sys.path_importer_cache (invalid namespace import)


I have a weird phenomenon (Windows, python 3.8.5): I have a folder: c:\myfolder. Somehow this folder got into the sys.path_importer_cache, and now every subfolder in c:\myfolder can be imported as namespace packages. So for example, there is this subfolder: c:\myfolder\foo\ And if I try to import foo, I get this:

import foo
foo
<module 'foo' (namespace)>
foo.__path__
_NamespacePath(['c:\\myfolder\\foo'])

and its loader is a NamespaceLoader.

The problem is, that foo itself is a package, that is installed into the site-packages, and now it does not get imported from there, because this weird namespace package beats it.

What have I messed up? Where do I look? I could only find this hint, that c:\myfolder is in the sys.path_importer_cache. But why did it get there? How can I remove it? (I tried importlib.invalidate_caches(), no success...) One more hint: if I run python in isolated mode (python -I), this problem goes away. However, I don't have anything in my user-site that could cause it (as far as I know.)

Any ideas? Thanks!


Solution

  • It has been figured out... I python in Anaconda, and I have launched the Anaconda Prompt (with condabin/activate) in the c:\myfolder as start folder. That's why it got into the path_importer_cache... If I start it simply in c:, the problem goes away.