在不同于Jupyter Noteb的环境中运行python脚本

2024-09-30 12:33:51 发布

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

我有一个主要的Jupyter笔记本文件,在切片机内核上运行。它打开切片器并让用户保存标签。我知道如何在pythor3内核上运行PyTorch的深度学习模型。为此,我首先需要将标签转换为我需要的格式,即tiff。你知道吗

我在Spyder成功了。你知道吗

import numpy as np
from imageio import imwrite
import os

## define where the masks are saved as a .npy
labelpath="../temp/label.npy" # changed for privacy
## define path where the new masks should be saved as .tiff files
labelpathsave="../temp/" # changed for privacy

## open the object
label=np.load(labelpath)

number_files=np.shape(label)[0]

for i in range(0, number_files):
   string="label"+str(i+1)+".tiff"
   labelpath=os.path.join(labelpathsave, string)
   currentlabel=label[i]
   imwrite(labelpath, currentlabel)

上面的方法是可行的,但是尝试在Jupyter笔记本中执行这个python文件。你知道吗

execfile('../scripts/Maja/transform_label.py') # changed for privacy

我得到“ImportError:没有名为imageio的模块”。你知道吗

我如何才能让它工作执行“转换”_标签.py“考虑到它在不同的环境中运行?你知道吗

(我需要在单独的文件中完成,因为我无法让PyTorch包在切片器内核上工作)


Tags: 文件theimportforasnpjupyterfiles
2条回答

我认为脚本不起作用,因为找不到imageio模块。 您是否尝试过将imageio保存在一个目录中,您知道Jupyter笔记本可以加载其他模块?也许只是目录的问题,jupyter笔记本在哪里寻找模块。你知道吗

也可能是spyder(如果您通过anaconda发行版使用它)预装了这个imageio包。所以你也可以尝试,只要在jupyter检查,如果你有安装在那里的软件包。你知道吗

安装imageio

对于python 2

pip install imageio  user

对于python 3

pip3 install imageio  user

用于康达环境

conda install -c conda-forge imageio

注意:必须首先检查笔记本上运行的是哪个版本的python。你知道吗

安装后安装包括导入所需的所有必要库) 重新运行上面的代码!!!。。。你知道吗

相关问题 更多 >

    热门问题