gpt4 book ai didi

python - 导入错误 : No module named _mysql

转载 作者:太空狗 更新时间:2023-10-29 11:17:44 27 4
gpt4 key购买 nike

我正在尝试使用 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

当我研究这个时,我继续挖掘许多不适用于 amazon linux 的 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/

最佳答案

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

您(通常,并非总是)可以做什么:

  1. 转向您正在寻找的 python 模块工作的系统
  2. 确定它的“位置”,比如在我的ubuntu上安装enum34后,安装后会把文件放到/usr/lib/python2.7/dist-packages/enum
  3. 将该目录放入存档
  4. 在您的“目标”系统上,在本地提取该存档
  5. 操纵 python 路径以包含本地提取的存档

如前所述,这并不漂亮;但如果没有更好的答案出现;你至少可以尝试一些东西......

关于python - 导入错误 : No module named _mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148410/

27 4 0