Jython subprocess.call() to Python

点点圈 提交于 2019-12-08 05:43:53

问题


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

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

But when I call the script from within my Jython program being built in 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?

Thanks in advance.

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

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.


回答1:


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

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 indeed. Aneurysm averted! Upvotes to this question for a hint.




回答2:


From your tracelog, the path you configured is wrong for Jython. You should use Jython's os module instead of Python2.7's.

Python imports every module only once, so

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. 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.

Please correct the path.

Or, you could use JyDT or something instead :)



来源:https://stackoverflow.com/questions/9492246/jython-subprocess-call-to-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!