简体   繁体   English

当父目录位于 sys.path 中时,Python 无法始终找到模块

[英]Python can't consistently find a module while the parent directory is in sys.path

I'm currently working on a project where I need to import self made modules that could be anywhere on the filesystem.我目前正在做一个项目,我需要导入可能位于文件系统任何位置的自制模块。 But python can't seem to consistently import them.但是 python 似乎无法始终如一地导入它们。

All the modules have the same name which is why I couple them with their parent directory, like so: xyz.abc for a module called abc.py in a folder called xyz .所有模块都具有相同的名称,这就是为什么我将它们与它们的父目录结合起来,如下所示: xyz.abc用于名为xyz的文件夹中名为abc.py的模块。 xyz's parent directory is added to path. xyz 的父目录被添加到路径中。 To import them later on, I use importlib.import_module(module_name) where module name is xyz.abc .为了稍后导入它们,我使用importlib.import_module(module_name)其中模块名称为xyz.abc

To test my code, I am using python's tempfile lib to create temporary directories and temporary files.为了测试我的代码,我使用 python 的 tempfile 库来创建临时目录和临时文件。 ( /tmp on my system is cleaned at reboot and not automatically.) (我系统上的/tmp在重新启动时被清理,而不是自动清理。)

I've tried adding an __init__.py file, it doesn't change anything.我试过添加一个__init__.py文件,它不会改变任何东西。 I've also tried os.sync() to force write the file to my disk thinking it might be a race problem but it didn't help.我也尝试过os.sync()强制将文件写入我的磁盘,认为这可能是一个竞争问题,但它没有帮助。

Here's the code where it breaks:这是它中断的代码:

if parent not in sys.path:
    sys.path.insert(0, parent)
module_name = os.path.basename(dir_path) + "." + filename.split(".")[0]
return importlib.import_module(module_name)

The tree structure of the directory is:目录的树结构为:

/tmp/tmp9tp_1j_q/
└── tmprvbxhk3t.py

And sys.path has '/tmp' in it when I print it right before module_name =... .当我在module_name =...之前打印sys.path时,其中包含'/tmp'

When executing the code, sometimes it works and sometimes it doesn't .执行代码时,有时有效,有时无效 I get: ModuleNotFoundError: No module named 'tmpfjig62pf'我得到: ModuleNotFoundError: No module named 'tmpfjig62pf'

I'm thinking maybe sys.path isn't internally updated directly after an insert.我在想也许sys.path在插入后没有直接在内部更新。

I need some help to solve this problem.我需要一些帮助来解决这个问题。

The problem isn't in sys.path , but it's close.问题不在sys.path中,但很接近。 It is the Finder object in sys.meta_path that uses sys.path and sys.path_hooks to look for modules, and sys.meta_path has internal caches that aren't "refreshed" automatically in such a situation. sys.meta_path 中的 Finder sys.meta_path使用sys.pathsys.path_hooks来查找模块,并且sys.meta_path具有在这种情况下不会自动“刷新”的内部缓存。

So, to successfully import a module that's been created after the interpreter began execution, the internal caches of sys.meta_path should be invalidated.因此,要成功导入解释器开始执行后创建的模块, sys.meta_path的内部缓存应该无效。

That can be achieved by calling importlib.invalidate_caches() after creating the test modules.这可以通过在创建测试模块后调用importlib.invalidate_caches()来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在sys.path中存在的本地目录中找不到模块 - Can't find module in local dir, present in sys.path 推荐sys.path但仍然找不到模块 - ammend sys.path but still can't find a module 无法导入 Python 模块,因为“sys.path”和“sys.executable”存在差异 - Can't import Python module because Difference in “sys.path” and “sys.executable” 如何将脚本的父目录作为模块加载,而无需将该目录的父目录的所有同级添加到sys.path中? - How can I load the parent directory of a script as a module, without adding all of the siblings of that directory's parent directory to my sys.path? Python可以配置为缓存sys.path目录查找吗? - Can Python be configured to cache sys.path directory lookups? 无法重新加载另一个文件夹中的模块,也无法重新加载sys.path中的模块 - Can't reload module that is in another folder but also in sys.path 为什么鼻子测试不能在sys.path中找到一个元素? - Why can't nosetests find one the elements in sys.path? 为什么 Python 找不到 sys.path 目录中的共享对象? - Why can't Python find shared objects that are in directories in sys.path? 当模块位于sys.path中时,Python ImportError - Python ImportError when module is in sys.path 没有名为error python的模块,是否在sys.path上? - no module named error python, is it on sys.path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM