Python:ImportError:/usr/local/lib/python2.7/lib-dynload / _io。所以:未定义符号:PyUnicodeUCS2_Replace

时间:2022-11-30 20:17:41

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:

我正在尝试构建一个triviol Python脚本,它将从URL获取数据并将其保存到服务器上。Concider以下代码:

#!/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.

我查看了SO并发现了类似的错误,但是它们似乎没有理解为什么有些人能够运行这个代码,而我没有。他们似乎都在处理C语言中开发的问题,并使用Python来访问C模块。

Is it the Ubuntu version, Python version??

是Ubuntu版本,Python版本吗?

Thank you.

谢谢你!

1 个解决方案

#1


17  

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.

您已经(至少)安装了两个不同版本的Python,并且您正在混合它们的文件。确保$PYTHONPATH, $PYTHONHOME和sys。路径只包含用于单个Python安装的文件夹。在您的情况中,一个安装在/usr/local,另一个可能在/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.

此外,您可以尝试安装virtualenvwrapper并设置独立的python环境,以减轻您可能遇到的任何冲突。这里有一个安装和使用virtualenv的教程。

#1


17  

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.

您已经(至少)安装了两个不同版本的Python,并且您正在混合它们的文件。确保$PYTHONPATH, $PYTHONHOME和sys。路径只包含用于单个Python安装的文件夹。在您的情况中,一个安装在/usr/local,另一个可能在/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.

此外,您可以尝试安装virtualenvwrapper并设置独立的python环境,以减轻您可能遇到的任何冲突。这里有一个安装和使用virtualenv的教程。