新的python 3.7 / django 2.2.1安装无法识别已安装mysqlclient

时间:2019-05-13 15:06:43

标签: mysql django python-3.x macos pipenv

我有一个全新的django 2.2.1项目,我刚刚将其安装到OS X(10.14.4)上的python 3.7 virtualenv中。经过一番无奈之后,我安装了mysqlclient,但是当我运行django dev服务器时,它无法识别它已安装:

这是我到目前为止采取的步骤:

brew install mysql
pipenv --three
pipenv install django==2.2.1
pipenv install mysqlclient
brew uninstall mysql
brew install mysql-connector-c
pipenv install mysqlclient
brew unlink mysql-connector-c
brew install mysql
django-admin startproject projectname

现在,对现成的django安装所做的唯一更改是将默认数据库后端更改为django.db.backends.mysql,当我运行django服务器时,我得到以下信息: / p>

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

但是,回到virtualenv并执行“ pip install mysqlclient”会给出:

Requirement already satisfied: mysqlclient in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages (1.4.2.post1)

有什么想法为什么django看不到virtualenv中安装的mysqlclient?我可以确认上述所有操作均已在virtualenv中运行。我怀疑它与OSX让您安装它的方式有关,但是我不确定如何将其分开。

我也曾尝试replace mysqldb with pymysql as suggested here,但这导致服务器发出另一个错误:

raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

Mysql是该项目的必需项,因此不能选择使用备用后端。非常感谢您提供的任何帮助,在此先感谢您。

更新:点冻结的输出:

Django==2.2.1
django-auth-ldap==1.7.0
Markdown==3.1
mysql-connector-python==8.0.16
mysqlclient==1.4.2.post1
protobuf==3.7.1
pyasn1==0.4.5
pyasn1-modules==0.2.5
PyMySQL==0.9.3
pyodbc==4.0.26
python-ldap==3.2.0
pytz==2019.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
xmltodict==0.12.0

更新2: 在conor的帖子之后(感谢conor),我重新开始了一个全新的virtualenv,并再次成功安装了mysqlclient1.4.2.post1。这是pip冻结的输出:

Django==2.2.1
mysqlclient==1.4.2.post1
pytz==2019.1
sqlparse==0.3.0

虽然当我执行python manage.py runserver时仍然出现相同的错误:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

更新3: 以为我会尝试在python命令行上导入MySQLdb模块,以查看是否可以获得更多了解,并且它返回以下内容:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Expected in: flat namespace
 in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so

3 个答案:

答案 0 :(得分:2)

如果您使用macOS,请执行此操作

$ brew uninstall mysql
$ brew install mysql-connector-c
$ brew unlink mysql-connector-c
$ brew install mysql
$ pip install mysql-python

并按照此处的说明进行操作:https://pypi.org/project/mysqlclient/

答案 1 :(得分:1)

已解决

所以问题似乎在于,在初始安装时,mysqlclient库是针对错误版本的mysql编译的(不确定如何发生),因此我不得不强制其重新编译。

以下是步骤:

brew uninstall mysql
brew uninstall myysql-connector-c
pipenv uninstall mysqlclient
brew install mysql-connector-c

这时,我们需要按照conor链接到的指令更新/ usr / local / bin / mysql_config(再次感谢conor),即更改读取的行

libs="$libs -l "

libs="$libs -lmysqlclient -lssl -lcrypto "

然后,to fix the resultant "library not found for -lssl" error I used the answer from this question

export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

然后最终迫使mysqlclient重新编译并重新安装mysql:

pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient
brew unlink mysql-connector-c
brew install mysql

感谢所有花时间帮助的人!

答案 2 :(得分:0)

从python 3.7.4降级到python 3.6在Windows 10上为我解决了这个问题。