添加为子目录时找不到导入

2024-06-16 08:42:08 发布

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

我已经克隆了https://github.com/sarathknv/adversarial-pytorch。档案一号_像素.py查找所有导入,但如果我将敌对Pythorch作为子目录添加到现有项目-myproject->;other->;Antarial Pythorch,则会收到错误:

from model import BasicCNN

导致错误:

Unresolved reference BasicCNN

我已尝试将导入行修改为:

^{pr2}$

但这并不能解决问题。

如何导入作为子项目存在的python文件?


Tags: 项目pyhttpsgtgithubcom错误像素
3条回答

您需要向每个要从中导入模块的目录中添加\uu init_uu.py文件。 祝你好运!在

您可能需要将“myproject/other”添加到系统路径中,如下所示:

import sys
sys.path.append('yourpathstring')
from model import BasicCNN

我应该提到我在用PyCharm。这也解决了以下问题:

从PyCharm:“File”->;“Settings”->;“Project Structure”->;添加myproject/other作为源文件夹。在

相关问题 更多 >