简体   繁体   中英

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.

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.

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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