问题描述:

I have a python module (getActiveLocation.py). It basically makes a http POST call, processes its response and returns it.

It makes use of json module. I am running python 2.6 on RedHat Linux. This module works fine when it is run standalone.

When the module getActiveLocation.py is run standalone, sys.path is

['/current_directory/','/usr/lib64/python26.zip','/usr/lib64/python2.6','/usr/lib64/python2.6/lib-tk','/usr/lib64/python2.6/site-packages','/usr/lib/python2.6/site-packages']

json module is existing within /usr/lib64/python2.6/ directory.

This python module (getActiveLocation.py) is imported inside a jython script (schedule_location.py) and when jython script is run, it is giving an import error for json module.

ImportError: no module named json

It looks like a sys.path issue.

I tried manually adding /usr/lib64/python2.6 to sys.path inside schedule_location.py via:

sys.path.append('/usr/lib64/python2.6')

Then the error changed to:

File "/usr/lib64/python2.6/json/__init__.py", line 108

from .decoder import JSONDecoder

^

SyntaxError: invalid syntax

Any idea why this error is happening? Your help is appreciated.

Vasily,

I am on a production system where I do not have any control! So upgrading jython is not an immediate solution for me :(.

I used simplejson as mentioned in the comment in Portable json module in jython

But here again, it is working when I run it in python. But when run from jython, it gives:

File "schedule_location.py", line 21, in ?

File "getActiveLocation.py", line 4, in ?

File "simplejson/__init__.py", line 113, in ?

File "simplejson/decoder.py", line 7

from .compat import fromhex, b, u, text_type, binary_type, PY3, unichr

^

SyntaxError: invalid syntax

To overcome this, I used:

from __future__ import absolute_import

But it gave:

File "schedule_location.py", line 21, in ?

File "getActiveLocation.py", line 1

SyntaxError: future feature absolute_import is not defined

I understand that this requires python 2.5 or more. But I am on Python 2.6

网友答案:

Probably you need

from __future__ import absolute_import

to make your code compatible with Python 3.x style imports. Or try to remove "dot": from decoder import JSONDecoder. It should work in Python 2.6. Not sure about Jython.

EDIT: Found answer in the comment here: Portable json module in jython

网友答案:

I had the same SyntaxError when running a python script from a bash script. The solution was to specify the PYTHONPATH, e.g. PYTHONPATH = /usr/local/lib/python2.7. I also specified which python to use when running the python script, e.g. /usr/local/bin/python my_python_script.py.

相关阅读:
Top