ImportError: attempted relative import with no known parent package
with No import package Parent ImportError Relative known
2023-09-27 14:19:49 时间
问题背景
最近开始尝试在终端训练模型时遇到了这个问题。原先的项目架构如下(这里为了方便举了个最简单的例子):
.
├── model
│ └── model1.py
├── process.py
└── train.py
1 directory, 3 files
其中 process.py
中的内容为
def get_data():
return 0
model1.py
中的内容为
from ..process import get_data
def get_model():
return get_data() + 1
train.py
中的内容为
from .process import get_data
from .model.model1 import get_model
print(get_data(), get_model())
随后在终端执行 python train.py > ./train.log 2>&1
时出现了如文章标题所提到的bug:
Traceback (most recent call last):
File "/root/autodl-tmp/project/train.py", line 1, in <module>
from .process import get_data
ImportError: attempted relative import with no known parent package
解决方案
去掉前面的 .
,即:
把 model1.py
的内容改为
from process import get_data
def get_model():
return get_data() + 1
把 train.py
中的内容为
from process import get_data
from model.model1 import get_model
print(get_data(), get_model())
就可以了。
注意,这样做虽然可以成功执行 python train.py
,但是执行 python model/model1.py
依然会报错,此时采用 python -m model.model1
。
相关文章
- 解决No converter for [class java.util.ArrayList] with preset Content-Type ‘null‘问题
- qt ISO C++ forbids declaration of 'XXXX' with no type
- ssh连远程centos服务器时,提示报错Unable to negotiate with **** port 22: no matching host key type found,连不上。
- Error: No named parameter with the name 'shadowThemeOnly'.
- A simple dynamic library implemented in C# 4.0 to deal with XML structure
- How To run OAI eNB (No S1) with USRP X310(1)
- Instance Segmentation with Mask R-CNN and TensorFlow
- how to deal with "no such file error or diretory" error for a new programmer in QT creator
- TRUNCATE can't with condition
- with torch.no_grad()
- postgres WITH 查询
- cocos2d-x于android在call to OpenGL ES API with no current context
- Alibaba Cloud and Red Hat Join Forces to Bring Increased Flexibility with Open Source
- 已解决No handles with labels found to put in legend
- files found with path ‘assets/location_map_gps_locked.png’ from inputs
- Unknown failure: Error: java.lang.IllegalStateException: No storage with enough free space; res=-1
- No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
- A tuple with one element requires a comma in Python
- 解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"错误
- 【ybt高效进阶4-2-4】【POJ 3468】区间修改区间查询 / A Simple Problem with Integers
- iframe跨域问题:Uncaught DOMException: Blocked a frame with origin解决方法
- ASM Diskgroup Creation Failed with "IBM AIX RISC system/6000: 6:no such device or address" [ID 12634