Discuss / Python / 安装 mysql-connector-python 遇到的坑

安装 mysql-connector-python 遇到的坑

Topic source

xian_wen

#1 Created at ... [Delete] [Delete and Lock User]

windows 直接去官网安装 mysql-connector-python 最新版

安装之后,base 环境中有 mysql-connector-python,但自建环境 py39 中没有:

(base) $ conda list mysql-connector-python
# packages in environment at C:\Anaconda3:
#
# Name                    Version                   Build  Channel
mysql-connector-python    8.0.25                   pypi_0    pypi
(base) $ conda deactivate
$ conda activate py39
(py39) $ conda list mysql-connector-python
# packages in environment at C:\Anaconda3\envs\py39:
#
# Name                    Version                   Build  Channel

尝试conda install安装,失败,要求 python 版本低于 3.8:

...
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - mysql-connector-python -> python[version='>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']

Your python: python=3.9
...

尝试pip install安装,失败,我在 Pycharm 中 Install Package 也会失败,失败原因相同:

(py39) $ conda install pip
(py39) $ pip install mysql-connector-python
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Anaconda3\envs\py39\lib\site-packages\pip\_internal\cli\base_command.py", line 180, in _main
    status = self.run(options, args)
...
  File "C:\Anaconda3\envs\py39\lib\ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

pip 换源安装,成功,在 Pycharm 下面的 Terminal 中(默认为 (venv) 环境)输入以下命令也同样安装成功:

(py39) $ pip install mysql-connector-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
(py39) $ conda list mysql-connector-python
# packages in environment at C:\Anaconda3\envs\py39:
#
# Name                    Version                   Build  Channel
mysql-connector-python    8.0.25                   pypi_0    pypi

关于源的选择,也遇到一些坑:

# 安装成功的源:
# 阿里云:http://mirrors.aliyun.com/pypi/simple/
# 豆瓣:http://pypi.douban.com/simple/

# 安装失败的源,凡是 https 都安装失败了,无论加不加 --trsuted-host:
# 清华:https://pypi.tuna.tsinghua.edu.cn/simple/
# 阿里云:https://mirrors.aliyun.com/pypi/simple/
# 豆瓣:https://pypi.douban.com/simple/

最后,PyCharm 在 Python Interpreter 中也可以添加以上源,Linuxpip install也可以更换以上源,换源不仅可以解决某些包的安装问题,同时也能大大提高包的安装速度。

pip install中传入入参数-i可用于包的临时安装,一劳永逸的方法是命令行中直接输入:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

或者手动配置pip.conf文件:

# https 源 pip.conf 配置:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

# http 源 pip.conf 配置:
[global]
index-url = http://pypi.douban.com/simple/
[install]
trusted-host = pypi.douban.com

查看pip config配置:

$ pip config list
global.index-url='http://pypi.douban.com/simple/'
install.trusted-host='pypi.douban.com'

配置之后安装包只需要:

$ pip install mysql-connector-python

windows 中我一般用conda install安装,所以没有进行配置。Linux(我是 WSL)因为实在忍受不了pip install的龟速(百兆宽带下载速度竟然只有 20KB/s 左右),所以进行了换源配置。


  • 1

Reply