简体   繁体   English

在与Jupyter Notebook不同的环境中运行python脚本

[英]Run a python script in a different environment from a Jupyter Notebook

I have a main Jupyter Notebook file that operates on a Slicer kernel. 我有一个在切片器内核上运行的Jupyter Notebook主文件。 It opens slicer and makes the user save the labels. 它打开切片器并使用户保存标签。 I know what to run a deep learning model in PyTorch on a Python 3 kernel. 我知道如何在Python 3内核上的PyTorch中运行深度学习模型。 To do this, I first need to transform the labels to the format that I need which is tiff. 为此,我首先需要将标签转换为所需的tiff格式。

I managed to do it in Spyder. 我设法在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)

This above works, but trying to execute this python file in the Jupyter Notebook. 上面的方法有效,但是尝试在Jupyter Notebook中执行此python文件。

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

I get "ImportError: No module named imageio". 我收到“导入错误:没有名为imageio的模块”。

How can I make it work execute "transform_label.py" given that it runs in a different environment? 如果它在不同的环境中运行,我如何使其执行“ transform_label.py”?

(I need to do it in separate files because I cannot get the PyTorch package to work on a Slicer kernel) (我需要在单独的文件中执行此操作,因为无法使PyTorch软件包在Slicer内核上工作)

installing imageio 安装imageio

for python 2 对于python 2

pip install imageio --user

for python 3 对于python 3

pip3 install imageio --user

For Conda Environments 对于Conda环境

conda install -c conda-forge imageio

NOTE : Must check first which version of python is being running on Notebook. 注意 :必须首先检查Notebook上正在运行哪个版本的python。

After installation ( The installation includes all necessary lib that are needed to import .) Rerun the above code!!!... 安装后安装包括导入所需的所有必需的库 。)重新运行以上代码!

I think that the script is not working because the imageio module cannot be found. 我认为该脚本无法正常工作,因为找不到imageio模块。 Have you tried to save imageio in a directory where you know that other modules can be loaded by Jupyter notebooks? 您是否尝试过将imageio保存在一个目录中,而您知道Jupyter笔记本可以加载其他模块吗? Maybe it is just a matter of the directories, where jupyter notebooks looks for modules. 也许仅仅是目录的问题,jupyter笔记本在其中寻找模块。

Or it could also be that for spyder (if you use it through the anaconda distribution) had this imageio package preinstalled. 或者也可能是对于spyder(如果您通过anaconda发行版使用它)已预装了此imageio软件包。 So you could also try and just check in jupyter if you have the package installed there. 因此,您也可以尝试在jupyter中检查是否已安装软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM