背景
我使用Python2.7并按照www.scivision.dev上的说明安装了Imagej,没有任何问题。从终端运行时,该应用程序运行正常。
我使用的Java版本是
parovelb@Latitude-E6510:~$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)
OpenJDK的路径是在我的~/.bashrc
# java path
PATH=/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH
export PATH
问题
我在Python2.7中无法从脚本运行ImageJ宏。该脚本使用subprocess.Popen
调用ImageJ宏。宏会创建一个CSV文件,并将结果写入其中。这是我的脚本:
#!/usr/bin/python2.7 python2.7
#!/bin/sh -i
# -*- coding: utf-8 -*-
"""
This program runs an ImageJ macro.
"""
import os
import csv
import sys
import subprocess
from subprocess import PIPE, STDOUT
from collections import defaultdict
def Connector_Check():
cmd = ["imagej", "-macro", "luerlocktest.ijm"]
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutdata, stderrdata) = process.communicate()
print 'stdoutdata', stdoutdata
# read result in row 1, col 2
with open(" ~/results.csv","rb") as res:
resReader = csv.reader(res)
resReader = list(resReader)
val = resReader[1][2]
print "Connector check value = ", val
if __name__ == "__main__":
# execute connector check
Connector_Check()
如果我从终端运行命令imagej -macro luerlocktest.ijm
,它可以正常工作,但运行脚本会给我错误:
stdoutdata -macro: 1: -macro: imagej: not found
Traceback (most recent call last):
File "wrapper_imagej.py", line 34, in <module>
Connector_Check()
File "wrapper_imagej.py", line 25, in Connector_Check
with open("/home/parovelb/Desktop/CV/Results.csv","rb") as res:
IOError: [Errno 2] No such file or directory: '/home/parovelb/Desktop/CV/Results.csv'
不幸的是,以下这些常见问题解答无法解决我的问题:
关于为什么imagej: not found
的任何建议?
1。更新
使用Shell=False
会出现错误:
Traceback (most recent call last):
File "wrapper_imagej.py", line 34, in <module>
Connector_Check()
File "wrapper_imagej.py", line 20, in Connector_Check
process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
答案 0 :(得分:0)
使用subprocess
打开shell并不会使其执行.bashrc
,因此它将永远不会看到$PATH
的修改版本。您可以尝试将shell=False
参数添加到Popen()
调用中。其他选项包括以另一种方式将imagej添加到您的PATH
或使用程序本身的完整路径调用imagej,完全不需要$PATH
变量。
例如:
cmd = ["/usr/lib/jvm/java-11-openjdk-amd64/bin/java/imagej", "-macro", "luerlocktest.ijm"]
答案 1 :(得分:0)
我确实更改了cmd
的格式,并将ImageJ
的安装路径放置在$HOME/ImageJ/ImageJ
中。另外,我确实使用了单个命令行而不是列表。
不正确:
cmd = ["imagej", "-macro", "luerlocktest.ijm"]
正确:
cmd = ["$HOME/ImageJ/ImageJ -macro luerlocktest.ijm"]