从不同目录导入文件的方式之间的区别

Difference between ways to import files from different directory

根据这个 Question ,据说要使用本地文件夹中的库或文件,我们使用:

import sys
sys.path.append("/path/to/your/directory")

但我的疑问是:

import sys
sys.path.append("/path/to/your/directory")

和:

locals()['path'].append("/path/to/your/directory")

和:

globals()['path'].append("/path/to/your/directory")

导入目录文件的功能相同,还是有所不同?
(我还没有看到任何关于这个的讨论)

"Hacking" sys.path 启用导入是不好的做法。使用 editable installs 代替:

pip install --editable /path/to/your/directory

这会将您的项目的符号链接插入到站点包文件夹中,然后 允许 Python 正确找到您的包裹。