ImportError:没有名为_mysql的模块

时间:2016-08-25 14:47:49

标签: python linux amazon-web-services amazon-ec2 mysql-python

我正在尝试使用Python模块MySQL-python从运行amazon linux的AWS EC2实例连接到外部MySQL数据库。

这是我正在尝试运行的代码:

db=_mysql.connect(host="hostname",user="dbuser",passwd="dbpassword",db="database")
db.query("""SELECT id, field1 FROM test""")
r=db.store_result()
row = r.fetch_row()
print row

我已经用pip安装了python模块:

sudo pip install MySQL-python

当我运行脚本时,我收到以下错误消息:

Traceback (most recent call last):
  File "script.py", line 2, in <module>
    import _mysql
ImportError: No module named _mysql

当我研究这个时,我继续为Ubuntu / Debian linux挖掘大量不适用于amazon linux的解决方案。

如何在amazon linux上修复此错误并运行脚本?

此外,来自任何有经验的Linux用户观察/回答:使用amazon linux是否有任何优势,因为我尝试学习更多的Linux并选择AWS或者我会更好地使用Ubuntu / Debian图像?我不是一个经验丰富的linux用户,因为可能是问题的显示。

更新

我意识到在amazon linux服务器上安装包是不成功的。当我尝试通过pip运行安装时,这是完整的输出:

$ sudo pip install MySQL-Python
Collecting MySQL-Python
  Using cached MySQL-python-1.2.5.zip
Installing collected packages: MySQL-Python
  Running setup.py install for MySQL-Python ... error
    Complete output from command /usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-B1IkvH/MySQL-Python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-RNgtpa-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
    creating build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql55 -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
    unable to execute 'gcc': No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-B1IkvH/MySQL-Python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-RNgtpa-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-B1IkvH/MySQL-Python/

1 个答案:

答案 0 :(得分:1)

只有一种解决方法,但是在我无法轻松调用的情况下对我有用的解决方法&#34; sudo pip install&#34;。

你(通常,并非总是)可以做的事情:

  1. 转到您正在寻找的python模块的系统
  2. 确定其位置&#34;,例如,在我的ubuntu上安装enum34后,安装会将文件放在 /usr/lib/python2.7/dist-packages/enum
  3. 将该目录放入档案
  4. 在你的&#34;目标&#34;系统,提取本地存档
  5. 操纵python路径以包含本地提取的存档
  6. 如上所述,这并不美丽;但如果没有更好的答案;你至少要尝试一下......