使用pip3安装mysqlclient时出现问题

ffx8fchx  于 2022-11-21  发布在  Mysql
关注(0)|答案(7)|浏览(124)

我尝试用Django建立python 3.6环境。安装说明说我应该安装mysqlclient才能连接到mySQL。我得到了这个:

dennis@django:~$ sudo -H pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
    Complete output from command python setup.py egg_info:
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-build-4jiw3hvk/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-4jiw3hvk/mysqlclient/
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

mySQL已正确安装。mysql_config文件中应包含哪些内容?当我尝试升级pip3时,得到以下信息:

dennis@django:~$ sudo -H pip3 install --upgrade pip3
Collecting pip3
  Could not find a version that satisfies the requirement pip3 (from versions: )
No matching distribution found for pip3
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

在使用pip3安装mysqlclient之后,我注意到该模块安装在python3.5目录中,而不是python3.6目录中,因此当我尝试导入运行python3.6的MySQLdb模块时,没有找到它

dennis@django:~/python_db$ python
Python 3.6.0+ (default, Feb  4 2017, 11:11:46) 
[GCC 5.4.1 20161202] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'
>>>

然后我用python 3.5重新运行,找到了它。我如何用pip3把模块安装到python 3.6目录中?

pkwftd7m

pkwftd7m1#

如果您使用的是Mac OS,请尝试以下操作:
brew install mysql
如果您使用的是Ubuntu 14/16,请尝试以下操作:
sudo apt install libmysqlclient-dev
还有一个:
可以使用sudo pip3 install -U pip更新PIP 3

lxkprmvk

lxkprmvk2#

LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient
rhfm7lfc

rhfm7lfc3#

在我的案例中,通过执行以下操作解决了该问题:

sudo aptitude install libmysqlclient-dev

因为我使用的是Python 3.x,所以下面的代码也是必要的:

sudo aptitude install python3-dev
gfttwv5a

gfttwv5a4#

如果您在OSX上运行MAMP,请执行以下操作:

export PATH=$PATH:/Applications/MAMP/Library/bin
am46iovg

am46iovg5#

在Debian Stretch中,所需的软件包名称似乎已更改为default-libmysqlclient-dev
https://packages.debian.org/stretch/default-libmysqlclient-dev

e5nszbig

e5nszbig6#

您可以尝试将mysql安装到特定的python目录,例如w/ python 3.6

sudo python3.6 /usr/bin/pip3 install mysqlclient
mrfwxfqh

mrfwxfqh7#

查看这个老问题 (在查找其他内容时), 我发现问题可能是包含mysql_config的目录不在当前的$PATH中。显然,安装脚本就是用这个目录来查找它的。可能是“mysql to be installed”,但不在$PATH中-或者$PATH引用了错误的版本。

相关问题