简体   繁体   English

Jython subprocess.call()到Python

[英]Jython subprocess.call() to Python

I'm attempting to make use of a CPython library from a Jython program through a subprocess.call() to a python script. 我试图通过subprocess.call()到一个python脚本,从Jython程序中使用CPython库。

I can make the call through the Jython interpreter without any problem. 我可以毫无问题地通过Jython解释器进行调用。

[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

But when I call the script from within my Jython program being built in Eclipse/PyDev: 但是当我在我的Jython程序中调用脚本时,它是在Eclipse / PyDev中构建的:

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

Results in this: 结果如下:

    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'

Any suggestions on how I can bring my script running under PyDev in line with the result from the interpreter? 有关如何使我的脚本在PyDev下运行与解释器的结果一致的任何建议?

Thanks in advance. 提前致谢。

EDIT 1: I corrected my module imports to only use Jython libraries and the error persists. 编辑1:我更正了我的模块导入只使用Jython库并且错误仍然存​​在。

EDIT 2: After doing some more testing, it seems like the spawned instance of CPython is stuck using the PythonPath for Jython. 编辑2:在做了一些更多的测试后,似乎CPython的衍生实例被使用Python的Jython卡住了。 The allows me to call 'python --version' but import os fails killing my subscript. 允许我调用'python --version'但导入os失败杀死我的下标。

The problem was that PyDev/ Jython was passing JYTHONPATH as PYTHONPATH to the subprocess. 问题是PyDev / Jython将JYTHONPATH作为PYTHONPATH传递给子进程。

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. 解决方法是加载所有环境变量,将Python Path更改为Python 2.7的正确位置,并通过env参数将其传递给Popen。

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 indeed. Blergh确实。 Aneurysm averted! 动脉瘤避免了! Upvotes to this question for a hint. 提出这个问题的提示。

From your tracelog, the path you configured is wrong for Jython. 从您的tracelog中,您配置的路径对于Jython来说是错误的。 You should use Jython's os module instead of Python2.7's. 你应该使用Jython的os模块而不是Python2.7。

Python imports every module only once, so Python只导入一次模块,所以

File "/usr/lib/python2.7/dist-packages/site.py", line 2, in __boot
    import sys, imp, os, os.path

would definitely imports the os module from python2.7. 肯定会从python2.7导入os模块。 Then 然后

File "/home/sarwar/jython2.5.2/Lib/posixpath.py", line 216, in <module>
     if not os._native_posix:

could not found the correct attribute from Jython's os module. 无法从Jython的os模块中找到正确的属性。

Please correct the path. 请更正路径。

Or, you could use JyDT or something instead :) 或者,您可以使用JyDT或其他东西:)

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

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