ImportError: 找不到名为 _mysql 的模块。

3

我正在尝试使用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上不起作用,如何修复此错误并运行脚本?

另外,对于任何有经验的Linux用户观察/回答:在我尝试学习更多关于Linux和AWS时,使用Amazon Linux是否有任何优势,还是使用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/

2
我会检查当前正在运行的Python安装中安装的模块。 - grael
感谢您的建议 - 是的,该模块似乎没有成功安装。 pip install 的输出现在已粘贴在上面。 - goose
1个回答

1

这只是一个解决方法,但在我无法轻松调用“sudo pip install”的情况下,它对我有效。

您(通常不总是)可以做到以下几点:

  1. 转向一个系统,在该系统上您正在寻找的Python模块可用
  2. 确定其“位置”,例如,在我的Ubuntu上安装enum34后,安装会将文件放在/usr/lib/python2.7/dist-packages/enum
  3. 将该目录放入存档文件中
  4. 在“目标”系统上,本地提取该存档文件
  5. 操作Python路径以包括本地提取的存档文件

如上所述,这并不完美;但如果没有更好的答案出现,您至少有一些尝试的东西...


感谢您的建议。简而言之,这是将已安装的代码转储到另一个系统中,并根据需要修改路径。但安装过程难道不比这更复杂吗?难道没有可能会漏掉一些东西吗?您有使用AWS的经验吗?您会考虑使用Amazon Linux吗? - goose
正确;转储和修改;-) 对于一些Python模块,安装过程甚至可能包括编译。我的观点基本上是:这在过去帮助了我。我没有AWS的经验;我不能告诉你是否需要关注Amazon Linux! - GhostCat
谢谢。我想仔细考虑后,我会继续在Ubuntu上学习。经过更多阅读,我认为当亚马逊员工支持大型设置时,Amazon Linux将发挥其作用,而我不需要学习AWS / Linux。 - goose
嗯,我们总是可以从“商业角度”来看待这样的“决策”(我应该将我的学习时间“投资”在哪里)。在某种程度上,如果我在这里或那里花费了时间,总是有必要问一下“投资回报率”会是什么。 - GhostCat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接