本文介绍了Jython subprocess.call()到Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过一个pyproxy.call()到python脚本将Jython程序中使用一个CPython库。

I'm attempting to make use of a CPython library from a Jython program through a subprocess.call() to a python script.

我可以通过Jython解释器进行调用,没有任何问题。

I can make the call through the Jython interpreter without any problem.

[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_22
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('python /opt/fake.py', shell=True)
ok!
0

但是当我在我的Jython程序中调用脚本时, PyDev:

But when I call the script from within my Jython program being built in Eclipse/PyDev:

subprocess.call('python /opt/fake.py', shell=True)

结果如下:

    Traceback (most recent call last):
  File "/home/sarwar/jython2.5.2/Lib/site.py", line 62, in <module>
    import os
  File "/home/sarwar/jython2.5.2/Lib/os.py", line 50, in <module>
    import posixpath as path
  File "/home/sarwar/jython2.5.2/Lib/posixpath.py", line 216, in <module>
    if not os._native_posix:
AttributeError: 'module' object has no attribute '_native_posix'

关于如何使我的脚本在PyDev下运行的建议符合解释器的结果?

Any suggestions on how I can bring my script running under PyDev in line with the result from the interpreter?

提前感谢

编辑1:
我将我的模块导入更正为仅使用Jython库,错误仍然存​​在。

EDIT 1:I corrected my module imports to only use Jython libraries and the error persists.

编辑2:
进行了一些更多的测试之后,似乎是使用PythonPath for Jython来阻止CPython的生成实例。允许我调用'python --version',但导入os不能杀死我的下标。

EDIT 2:After doing some more testing, it seems like the spawned instance of CPython is stuck using the PythonPath for Jython. The allows me to call 'python --version' but import os fails killing my subscript.

推荐答案

问题是PyDev / Jython将JYTHONPATH作为PYTHONPATH传递到子进程。

The problem was that PyDev/ Jython was passing JYTHONPATH as PYTHONPATH to the subprocess.

修复是加载所有环境变量,将Python Path更改为Python 2.7的正确位置,并通过env参数传递给Popen。

The fix was to load all the environment variables, change the Python Path to the correct spot for Python 2.7 and pass it to Popen through the env argument.

cmd = 'python /opt/fake.py'
my_env = os.environ
my_env["PYTHONPATH"] = '/usr/lib/python2.7/'
proc = subprocess.Popen(cmd ,bufsize=0, executable=None, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=None, close_fds=True, shell=True, env=my_env)
out = str(proc.communicate(proc.stdout))

Blergh确实。动脉瘤避免了提醒,以提示。

Blergh indeed. Aneurysm averted! Upvotes to this question for a hint.

这篇关于Jython subprocess.call()到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:49