Python: ImportError: /usr/local/lib/python2.7/lib-

2020-02-11 02:18发布

问题:

I am trying to build a triviol Python script that will grab data from URL and save it onto the server. Concider the below code:

#!/usr/bin/python
import pprint
import json
import urllib2

def getUSGS_json():
    print "Fetch data from URL"

    fileName = 'data/usgsEarthquacks_12Hrs.json'
    url = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson'
    data = urllib2.urlopen(url).read()

    if data:
        try:
            with open(fileName) as jsonGetData:
                filePut = open(fileName, 'w+')
                #add data
                filePut.write(data)
                filePut.close()

                j = json.load(jsonGetData)
                print j
        except Exception, e:
            print e
            raise
        else:
            pass
        finally:
            pass
    #end if
#end getUSGS_json

getUSGS_json()

Upon running the script I get the following errors:

Traceback (most recent call last):
  File "geoJsonFetch.py", line 4, in <module>
    import urllib2
  File "/usr/local/lib/python2.7/urllib2.py", line 94, in <module>
    import httplib
  File "/usr/local/lib/python2.7/httplib.py", line 79, in <module>
    import mimetools
  File "/usr/local/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/usr/local/lib/python2.7/tempfile.py", line 32, in <module>
    import io as _io
  File "/usr/local/lib/python2.7/io.py", line 51, in <module>
    import _io
ImportError: /usr/local/lib/python2.7/lib-dynload/_io.so: undefined symbol: PyUnicodeUCS2_Replace

I have looked around on SO and found similar errors like this one, but they do not seem to get at the heart of why some people are able to get this code to run and I am not. They all seem to be dealing with issues concerning developing in C and using Python to access that C module.

Is it the Ubuntu version, Python version??

Thank you.

回答1:

You have (at least) two different versions of Python installed and you're mixing their files. Make sure that $PYTHONPATH, $PYTHONHOME and sys.path only contain folders for a single Python installation. In your case, one installation is in /usr/local and the other is probably in /usr.

Also, you can try installing virtualenvwrapper and setting up separate python environment to alleviate any conflicts you might be having. Here is a tutorial for installing and using virtualenv.