无法从另一个python文件(在另一个目录中)调用函数

2024-04-30 05:46:05 发布

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

在ubuntu上,我使用以下结构:

.
├── src
│   ├── __init__.py
│   └── main.py
└── test
    ├── __init__.py
    └── test.py

main.py包含以下函数:

def fun(x,y):
    return x+y

而且test.py

from src.main import fun
print(fun(2,3))

我在目录daddy中工作。所以pwd打印daddy

当我尝试调用test.py

$ python3 test/test.py
Traceback (most recent call last):
  File "test/test.py", line 1, in <module>
    from src.main import fun
ModuleNotFoundError: No module named 'src'

我有什么选择,才能做到这一点


我已经看过了与此相关的其他答案,但它们对我不起作用。。。主要是因为它们不像我的案例那样处理工作目录。


Tags: 函数frompytestimportsrc目录init