可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Trying to install mysqlclient using pip3 on Python 3.6.0
$ pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in get_config
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 54, in <listcomp>
libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup_posix.py", line 12, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/
Already did
brew install mysql-connector-c
But still getting this error
回答1:
This worked for me:
brew install mysql-connector-c
edit mysql_config
(locate it: which mysql_config
)
correct this in mysql_config:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
It shoud be:
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
brew info openssl
- and finally
pip3 install mysqlclient
回答2:
I resolved this installing libmysqlclient-dev first:
sudo apt-get install libmysqlclient-dev
Hope it works for you.
回答3:
After extensive troubleshooting, I found that the brew install mysql-connector-c
just does not work.
The problem stems from the system not being able to identify that mysql_config
is installed (which is what mysql-connector-c
essentially does). Homebrew's method (open for argument) of installing the mysql-connector-c
in its /Cellar/
and creating a symlink to the /usr/local/bin/
directory seems to be causing problems as Python tries to follow the symlink.
To resolve the issue I performed the following:
brew uninstall mysql-connector-c
- download/install MacOS X MySQL Connector/C from Oracle's MySQL site
- note: just download the
.dmg
, no need to be complicated here...
- restart MacOS Terminal (or iTerm2) for good measure
which mysql_config
- you should see the correct path
/usr/local/bin/mysql/bin/mysql_config
activate virtualenv
(if applicable)
pip install mysqlclient
There may be other ways to still use Homebrew, but this was the most straightforward solution I found.
Note that the mysqlclient
GitHub README.md also states that the C-developer headers for Python3 are needed. I assume that mysql-connector-c
includes those; however, should you run into more issues, you might also install the Xcode Developer CI Tools for good measure.
xcode-select --install
They include the C compiler and other developer utilities from Apple. Read more here.
回答4:
Came across this while developing a django-based website, and I needed to set up the website to communicate with a mysql database. I've not used homebrew to do any of my installations.
Since I'm doing the development on a MacOS with Anaconda installed, I discovered that there was an issue with Anaconda. Without knowing the details, I was able to install mysqlclient with conda install mysqlclient
Hope that helps someone else with this problem as well.
回答5:
For Python 2.7 on specific programs:
- sudo chown -R $USER /Library/Python/2.7
- brew install mysql@5.7
- brew install mysql-connector-c
- brew link --overwrite mysql@5.7
- echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
- sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
- pip install MySql-python
This solved all issues I was having running a program that ran on Python 2.7 on and older version of MySql
回答6:
Other options didn't work for me, but this worked for me:
For Python3:
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip3 install mysqlclient
For Python2:
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip2 install mysqlclient
or
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install mysqlclient
回答7:
Personally I recommend using pymysql
instead of using mysqlclient
. You can install pymysql
with:
pip install pymysql
And you might edit your SQLAlchemy URL to:
mysql+pymysql://username:passwd@hostname/database