我正在尝试从名为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
档案结构:
C:-----
Users----
trevo----
Desktop----
AI_physicist----
pytorch_net----
theory_learning----
以下是整个错误:
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:
# 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
任何帮助,我将失去什么将是非常感谢,谢谢!
发布于 2021-12-29 19:18:43
尝试将__init__.py
而不是init.py
放在该文件夹中。
或者:sys.path.append(f'{os.path.dirname(__file__)}\\folder_name')
,请看看我和你的sys.append
之间的区别。那应该总是有效的。记住,只有当脚本在主文件夹的子文件夹中包含main.py时,这些方法才能工作。
https://stackoverflow.com/questions/70523810
复制相似问题