使用管道运行外部程序并在 python 中传递参数

发布于 2024-11-28 15:21:27 字数 696 浏览 2 评论 0原文

我尝试过,但是当我尝试打印这些参数时,它不返回任何值。 我提交下面的代码:

script1 运行外部 python 程序 (script2)

#(...)
proc = subprocess.Popen(['export PYTHONPATH=~/:$PYTHONPATH;' +
    'export environment=/path/to/environment/;' +
    'python /path/to/my/program/myProgram.py',
    '%(date)s' %{'date':date}, '%(time)s' %{'time':time}],
    stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
#(...)

script2 正在由 script1 运行

#(...)
print sys.argv[0] #prints the name of the command
print sys.argv[1] #it suppose to print the value of the first argument, but it doesn't
print sys.argv[2] #it suppose to print the value of the second argument, but it doesn't
#(...)

I tried that, but when I try to print these arguments it returns no values.
I submit my code below:

script1 that runs external python program (script2)

#(...)
proc = subprocess.Popen(['export PYTHONPATH=~/:$PYTHONPATH;' +
    'export environment=/path/to/environment/;' +
    'python /path/to/my/program/myProgram.py',
    '%(date)s' %{'date':date}, '%(time)s' %{'time':time}],
    stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
#(...)

script2 that is being run by the script1

#(...)
print sys.argv[0] #prints the name of the command
print sys.argv[1] #it suppose to print the value of the first argument, but it doesn't
print sys.argv[2] #it suppose to print the value of the second argument, but it doesn't
#(...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-12-05 15:21:28

尝试这个版本的脚本 1:

proc = subprocess.Popen('python /path/to/my/program/myProgram.py %s %s' % (date, time),
                        stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True,  
                        env = {'PYTHONPATH': '~/:$PYTHONPATH',
                               'environment': '/path/to/environment/'})

如果它不起作用,它应该可以更容易地找到您的问题;但我认为会的。

Try this version of script 1:

proc = subprocess.Popen('python /path/to/my/program/myProgram.py %s %s' % (date, time),
                        stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True,  
                        env = {'PYTHONPATH': '~/:$PYTHONPATH',
                               'environment': '/path/to/environment/'})

It should make it easier to find your problem if it doesn't work; but I think it will.

南街女流氓 2024-12-05 15:21:28

Docs 说,当指定 shell=True 时,任何其他参数都被视为 shell 的参数,不听命令。要使其工作,只需将 shell 设置为 False 即可。我不明白为什么你需要它是真的。

编辑:我看到你想使用 shell 来设置环境变量。使用 env 参数来设置环境变量。

Docs say that when specifying shell=True any additional args are treated as args to the shell, not to the command. To make it work, just set shell to False. I don't see why you need it to be True.

edit: I see you want to use shell to set environment variables. Use the env argument to set the environment variables instead.

烈酒灼喉 2024-12-05 15:21:28
  1. 使用 Popenenv 参数传递环境变量:
  2. 除非必要,否则不要使用 shell=True。它可以是安全性
    风险(请参阅警告)

test.py:

import subprocess
import shlex
import datetime as dt
now=dt.datetime.now()
date=now.date()
time=now.strftime('%X')

proc = subprocess.Popen(shlex.split(
    'python /tmp/test2.py %(date)s %(time)s'%{'date':date,
                                         'time':time}),
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        env={'PYTHONPATH':'~/:$PYTHONPATH',
                             'environment':'/path/to/environment/'})

out,err=proc.communicate()
print(out)
print(err)

test2.py:

import sys
import os

print(os.environ['PYTHONPATH'])
print(os.environ['environment'])
for i in range(3):
    print(sys.argv[i])

产量

~/:$PYTHONPATH
/path/to/environment/
/tmp/test2.py
2011-08-09
17:50:04
  1. Use Popen's env parameter to pass environment variables:
  2. Don't use shell=True unless you have to. It can be a security
    risk (see Warning)
    .

test.py:

import subprocess
import shlex
import datetime as dt
now=dt.datetime.now()
date=now.date()
time=now.strftime('%X')

proc = subprocess.Popen(shlex.split(
    'python /tmp/test2.py %(date)s %(time)s'%{'date':date,
                                         'time':time}),
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        env={'PYTHONPATH':'~/:$PYTHONPATH',
                             'environment':'/path/to/environment/'})

out,err=proc.communicate()
print(out)
print(err)

test2.py:

import sys
import os

print(os.environ['PYTHONPATH'])
print(os.environ['environment'])
for i in range(3):
    print(sys.argv[i])

yields

~/:$PYTHONPATH
/path/to/environment/
/tmp/test2.py
2011-08-09
17:50:04
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文