python模块的安全参考是什么?

2024-04-20 01:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想问一个关于如何正确引用自己的python模块的问题

我的文件结构如下:

src
   bin
      __init__.py
      main.py
   utils
      __init__.py
      foo.py
frontend
   front.py

直到现在,每当我运行python src/bin/main.py,其中有import src.utils.fooimport frontend.front行时,一切都很好,我的脚本运行正常

现在,我将代码从github克隆到另一台计算机上,当我做完全相同的事情时,我收到一个错误,说ModuleNotFoundError: No module named 'src'。我已经检查了print(sys.path)在python中使用的路径,看起来项目的根文件夹不再是路径的一部分,而是src/bin/is

为什么??我做错了什么?我怎样才能纠正它? 我想写一个可以克隆到任何计算机上的解决方案,所以我个人希望避免sys.path.append(absolute_path)


Tags: 模块pathpyimport路径srcbinfoo