首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python ModuleNotFoundError -没有名为“pytorch_net”的模块

Python ModuleNotFoundError -没有名为“pytorch_net”的模块
EN

Stack Overflow用户
提问于 2021-12-29 19:00:25
回答 1查看 127关注 0票数 1

我正在尝试从名为pytorch_net的文件夹中导入一个文件,从名为AI_physicist的文件夹导入一个名为models.py的脚本。我尝试更改文件的文件夹位置,将init.py文件转换为主AI_physicist文件夹,并更改sys.path.append命令以只获取其中包含文件的文件夹。我也看过其他职位,也尝试过解决办法,但都没有用。

以下是我找到并尝试过的帖子:What does os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) mean? python

import python module using sys.path.append

档案结构:

代码语言:javascript
运行
复制
C:-----
  Users----
     trevo----
        Desktop----
          AI_physicist----
            pytorch_net----
            theory_learning----

以下是整个错误:

代码语言:javascript
运行
复制
No module named 'pytorch_net'
File "C:\Users\trevo\OneDrive\Desktop\AI_physicist\theory_learning\models.py", line 13, in <module>
    from pytorch_net.net import MLP
  File "C:\Users\trevo\OneDrive\Desktop\AI_physicist\theory_learning\theory_model.py", line 24, in <module>
    from AI_physicist.theory_learning.models import Loss_Fun_Cumu, get_Lagrangian_loss

最后,这是我正在使用的代码:

models.py:

代码语言:javascript
运行
复制
# coding: utf-8

# In[ ]:


import numpy as np
import torch
import torch.nn as nn
from torch.autograd import grad
import sys, os
sys.path.append(os.path.join(os.path.dirname("__file__"), '..', '..'))
from AI_physicist.pytorch_net.net import MLP
from AI_physicist.pytorch_net.util import get_criterion, MAELoss, to_np_array, to_Variable, Loss_Fun
from AI_physicist.settings.global_param import PrecisionFloorLoss, Dt
from AI_physicist.theory_learning.util_theory import logplus

任何帮助,我将失去什么将是非常感谢,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-29 19:18:43

尝试将__init__.py而不是init.py放在该文件夹中。

或者:sys.path.append(f'{os.path.dirname(__file__)}\\folder_name'),请看看我和你的sys.append之间的区别。那应该总是有效的。记住,只有当脚本在主文件夹的子文件夹中包含main.py时,这些方法才能工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70523810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档