【问题标题】:Error installing mysqlclient for python on Ubuntu 18.04在 Ubuntu 18.04 上为 python 安装 mysqlclient 时出错
【发布时间】:2023-04-05 08:26:01
【问题描述】:

我在 Ubuntu 上安装了 Python 2.7.15rci 和 Python 3.6.7。当我在 virtualenv 上执行 'pip list' 时,它返回给我:

Django (2.1.5)
pip (9.0.1)
pkg-resources (0.0.0)
pytz (2018.9)
setuptools (39.0.1)
wheel (0.32.3)

我正在尝试安装 mysqlclient (pip install mysqlclient) 并返回错误。

  unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /home/david/env/project/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-pq18uxjj/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-y28h4ou0-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/david/env/project/include/site/python3.6/mysqlclient:
    /usr/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
    creating build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/MySQLdb
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,4,1,'final',0) -D__version__=1.4.1 -I/usr/include/mysql -I/home/david/env/project/include -I/usr/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/home/david/env/project/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-pq18uxjj/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-y28h4ou0-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/david/env/project/include/site/python3.6/mysqlclient" failed with error code 1 in /tmp/pip-build-pq18uxjj/mysqlclient/

所以,我尝试了不同的方法,例如:

sudo apt-get install python-dev
sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev

还有更多...但它们都不适合我,问题仍然存在。
有什么建议吗?

谢谢!

【问题讨论】:

  • 您需要根据文档安装default-libmysqlclient-dev,而不是libmysqlclient-dev
  • 我也安装了default-libmysqlclient-dev,但是还是不行……
  • 你说你试过sudo apt-get install python-dev python3-dev,那些还在安装吗?还是每次都卸载?所有这些都需要安装。
  • 这很奇怪,我刚刚在 Ubuntu 18.0.4 上使用默认 python (3.6) 安装它,没有任何问题。完全通过安装python3-dev gcc default-libmysqlclient-dev。这些就足够了。
  • 我不知道为什么我没有安装这些库,但我安装了libssl-dev 并且它起作用了!谢谢! ;)

标签:
mysql
django
python-3.x
pip
mysql-python