mypy 给出错误 即使添加 MYPYPATH 后也无法找到名为 math_func 的模块的实现或库存根

发布于 2025-01-10 10:27:40 字数 654 浏览 3 评论 0原文

我在包 'code' 下有两个模块。

math_func.py 中的第一个模块

是 main.py。

我正在 main.py 中导入 math_func.py。早些时候,当我运行 mypy ot 时,出现错误

'''无法找到名为“math_func”的模块的实现或库存根''',

但在阅读 mypy 文档后,我使用命令

(project_2) p0p029i@m-c02dt1k2md6n enviroment_2 % export 添加了路径 MYPYPATH MYPYPATH=/Users/p0p029i/Desktop/enviroment_2

i我再次遇到同样的错误。附上截图供参考。

main. py

math_func.py

i have two modules under package 'code' .

First module in math_func.py

another one is main.py.

I am importing math_func.py in main.py. Earlier when i was running mypy ot was giving error

'''Cannot find implementation or library stub for module named "math_func"'''

but after reading mypy docs i added path MYPYPATH using command

(project_2) p0p029i@m-c02dt1k2md6n enviroment_2 % export MYPYPATH=/Users/p0p029i/Desktop/enviroment_2

i am again getting the same error. Attached screenshot for reference.

main.py

math_func.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2025-01-17 10:27:41

math_func 不是顶级模块,因为 code/ 下有 __init__.py
因此,您应该改为编写 import code.math_func 或删除 code/__init__.py

environment_2 添加到 MYPYPATH 并不能解决问题,因为它仍然不允许 mypy 发现 math_func 作为顶级模块。

为了更好地理解它,请考虑以下导入之间的差异:

import .math_func      # relative import
import math_func       # look for a top-level module "math_func"
import code.math_func  # look for a top-level module "code" that has a submodule math_func underneath
import re              # look for a top-level module "re"

math_func is not a top-level module, as you have __init__.py under code/.
So you should write import code.math_func instead OR remove code/__init__.py.

Adding environment_2 to MYPYPATH does not solve it because it still doesn't allow mypy to discover math_func as a top-level module.

To have a better understanding of it, consider the difference between the following imports:

import .math_func      # relative import
import math_func       # look for a top-level module "math_func"
import code.math_func  # look for a top-level module "code" that has a submodule math_func underneath
import re              # look for a top-level module "re"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文