File "/app/PythonProjects/segment-anything-main/Import_test/down/test01.py", line 6, in <module> from ...segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor ImportError: attempted relative import with no known parent package
时间: 2023-12-14 19:36:16 浏览: 167
这个错误表明你在一个子模块中尝试使用相对导入,但是 Python 解释器无法确定当前模块的父级包。通常,这是因为你没有正确地设置 Python 模块的导入路径或包结构。
你可以尝试以下解决方案:
1. 将当前模块移动到正确的包目录中,确保包结构正确,或者在该模块中使用绝对导入语句而不是相对导入语句。
2. 使用绝对导入语句来代替相对导入语句。例如,如果你要导入 `segment_anything` 模块中的 `sam_model_registry`,可以使用以下语句:
```python
from segment_anything import sam_model_registry
```
3. 将当前模块作为脚本直接运行,而不是作为一个模块导入。如果你要运行 `test01.py`,可以使用以下命令:
```bash
python -m Import_test.down.test01
```
这会告诉 Python 将 `Import_test.down.test01` 视为一个脚本而不是一个模块,这可能会解决相对导入的问题。
相关问题
for i in `seq 2 15` do python3 image_crop.py --image_file ${dataroot}/$i/Ottawa-$i.tif --save_path ${save_folder}/train_image python3 image_crop.py --image_file ${dataroot}/$i/segmentation.png --save_path ${save_folder}/train_segment python3 image_crop.py --image_file ${dataroot}/$i/edge.png --save_path ${save_folder}/train_edge python3 image_crop.py --image_file ${dataroot}/$i/centerline.png --save_path ${save_folder}/train_centerline done
这是一个带有循环的 Bash 脚本,它的作用是对 `$dataroot` 目录下的 2-15 号 Ottawa-$i.tif 图像及其对应的 segmentation.png、edge.png 和 centerline.png 进行裁剪,将裁剪后的图像、分割图、边缘图和中心线图分别保存到 `${save_folder}/train_image`、`${save_folder}/train_segment`、`${save_folder}/train_edge` 和 `${save_folder}/train_centerline` 目录中。
python segment/train.py --data coco128-seg.yaml --weights yolov5s.pt --img 640 Traceback (most recent call last): File "segment/train.py", line 42, in <module> import segment.val as validate # for end-of-epoch mAP File "/home/sxj/opencv_yolov5/yolov5_7.0/segment/val.py", line 42, in <module> from models.common import DetectMultiBackend File "/home/sxj/opencv_yolov5/yolov5_7.0/models/common.py", line 28, in <module> from utils import TryExcept ImportError: cannot import name 'TryExcept' from 'utils' (/home/sxj/anaconda3/envs/yolo/lib/python3.7/site-packages/utils/__init__.py)什么问题怎么解决
看起来你的代码中使用了一个名为 TryExcept 的函数,但是 Python 解释器无法从 utils 模块中找到该函数。这可能是因为你的代码使用的是自己本地项目中的 utils 模块,而不是安装在系统中的公共 utils 模块。
你可以尝试在导入 TryExcept 函数的文件中添加以下代码,手动将项目目录添加到 Python 搜索路径中:
```python
import sys
sys.path.append('/path/to/your/project')
```
其中 `/path/to/your/project` 是你的项目目录路径。这样,Python 就能够找到你的 utils 模块中的函数了。
另外,你也可以检查一下 utils 模块中是否真的有 TryExcept 函数。如果没有,你需要修改你的代码,以使用正确的函数名。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![r](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)