简体   繁体   English

python脚本从命令行运行,但是使用子进程会出错

[英]python script runs from command line but using subprocess gives error

the following command line works and gives correct results 以下命令行有效并给出正确的结果

$ python maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001

but when the exact command is called, using the following subprocess module: 但是在调用确切命令时,请使用以下子流程模块:

run=subprocess.Popen([sys.executable, 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001' ])

it gives the following error: 它给出以下错误:

/usr/bin/python: can't open file 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001': [Errno 2] No such file or directory

what's the reason? 什么原因? the commands are exactly the same. 命令完全相同。 Thank you for your help. 谢谢您的帮助。

When using subprocess.Popen() the first argument should be a list with a separate entry for each argument to the process you want to run: 使用subprocess.Popen() ,第一个参数应该是一个列表,其中包含要运行的进程的每个参数的单独条目:

run=subprocess.Popen([sys.executable, 'maps2.py', '-i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0', '-o=temp/CMIP3', '-p=temp_001' ])

What you currently have would be the equivalent to running the following on the command line: 您当前拥有的内容等同于在命令行上运行以下内容:

python 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001'

In addition to FJ's answer, you can easily split the name of the executable from the arguments with shlex.split 除了FJ的答案,您还可以使用shlex.split从参数中轻松拆分可执行文件的名称。

mapsCommand = 'maps2.py -i=/media/babak/LaCie/necessary/visualisation/CMIP3_Babak/Temperature/bccr_bcm2_0 -o=temp/CMIP3 -p=temp_001'
fullCommand = [sys.executable]
fullCommand.extend(shlex.split(mapsCommand))
run=subprocess.Popen(fullCommand)

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

相关问题 Python 脚本从命令行运行良好,使用 Eclipse 和 PyDev 时找不到文件错误 - Python script runs fine from command line, file not found error when using Eclipse and PyDev Python子进程命令行错误 - Python subprocess command line error 使用python中的子进程模块运行带命令行输入的lua脚本 - Using the subprocess module in python to run a lua script with command line inputs 使用子进程在命令行中调用python脚本时运行困难 - Trouble running using subprocess to call python script in command line 在python子进程中使用exec查找命令会产生错误 - find command with exec in python subprocess gives error 从命令行运行时,在pycharm中运行的python脚本有内存分配错误 - python script that runs in pycharm has memory allocation error when running from command line Python脚本在命令行上运行,但不是从.sh文件运行 - Python script runs on command line but not from .sh file 使用子进程python获取命令行执行抛出的错误 - Get error thrown by command-line execution using subprocess python FASTQC在命令行上运行,但不在python子进程中运行(Java异常) - FASTQC runs on command line but not in python subprocess (Java exception) 使用Python的子进程模块运行带有命令行参数的程序 - Using the subprocess module of Python to run a program with arguments from the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM