ModuleNotFoundError:在 PyCharm 中导入存根文件时没有名为“stubs.profiler”的模块

问题描述 投票:0回答:2

在 Pycharm 中,我尝试在 .py 文件中导入存根文件(.pyi 文件),但出现错误

ModuleNotFoundError: No module named 'stubs.profiler'

我的文件结构如下:

image of my structure

在 profiler.py 中,我尝试使用

import stubs.profiler
来获取定义的类和函数,但出现错误
ModuleNotFoundError: No module named 'stubs.profiler'

我的文件内容如下,

VOP directory is in code directory

有人可以帮助我吗?

python pycharm stub pyi
2个回答
0
投票

您可以将“stubs”文件夹移动到主文件当前的工作目录,也可以通过在文件中写入以下代码来从任何路径运行所需的 .py 文件:

import sys
sys.path.append("Complete path to stubs folder")
import profiler

0
投票

存根文件 (.pyi) 存在,以便 PyCharm 和其他工具可以为您的 .py 文件显示正确类型的变量、返回值、函数参数等。它们不适合导入或以其他方式由 python 使用以任何方式解释器本身。

© www.soinside.com 2019 - 2024. All rights reserved.