Python 错误集锦

环境信息

  • Python 3.10.12

certificate verify failed: unable to get local issuer certificate

报错信息如下:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)
...
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)>

问题原因 : 本地 CA 证书不存在

解决方法

  1. 查看默认证书位置
    >>> import ssl
    >>> print(ssl.get_default_verify_paths())
    DefaultVerifyPaths(cafile=None, capath='/usr/local/openssl/ssl/certs', openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/usr/local/openssl/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/usr/local/openssl/ssl/certs')

    根据输出内容,可以看到 Python 使用的 openssl 位于 /usr/local/openssl/,CA 证书路径为 /usr/local/openssl/ssl/cert.pem,检查 CA 证书路径,发现 CA 证书不存在
    $ cd /usr/local/openssl/ssl/
    $ ls
    certs ct_log_list.cnf ct_log_list.cnf.dist misc openssl.cnf openssl.cnf.dist private
  2. 下载 CA 证书文件
    wget http://curl.haxx.se/ca/cacert.pem --no-check-certificate
    mv cacert.pem cert.pem
    下载 CA 证书文件后,重新尝试,SSl 连接正常。

can’t start new thread

环境信息

  • Docker 1.13
  • Python3.9

在 Docker 中运行 python 后,使用 pip 报错 RuntimeError: can't start new thread

# pip install --upgrade pip
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (23.0.1)
Collecting pip
Downloading pip-23.2.1-py3-none-any.whl (2.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/2.1 MB ? eta -:--:--ERROR: Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 160, in exc_logging_wrapper
status = run_func(*args)
File "/usr/local/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 247, in wrapper
return func(self, options, args)

...
File "/usr/local/lib/python3.9/site-packages/pip/_internal/operations/prepare.py", line 107, in get_http_url
from_path, content_type = download(link, temp_dir.path)
File "/usr/local/lib/python3.9/site-packages/pip/_internal/network/download.py", line 147, in __call__
for chunk in chunks:
File "/usr/local/lib/python3.9/site-packages/pip/_internal/cli/progress_bars.py", line 52, in _rich_progress_bar
with progress:
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/rich/progress.py", line 1169, in __enter__
self.start()
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/rich/progress.py", line 1160, in start
self.live.start(refresh=True)
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/rich/live.py", line 132, in start
self._refresh_thread.start()
File "/usr/local/lib/python3.9/threading.py", line 899, in start
_start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

[notice] A new release of pip is available: 23.0.1 -> 23.2.1
[notice] To update, run: pip install --upgrade pip

问题原因 Docker 版本太低,升级版本到 18.06 以上。参考说明

Exception: Can not find valid pkg-config name

使用 pip install 命令时报错:

Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually

在使用 pip install 安装某些依赖 MySQL 的 Python 包(例如 mysqlclient)时,可能会遇到类似 Exception: Can not find valid pkg-config name. 的错误。这通常是因为你的系统缺少必要的开发工具或库文件。

以下是解决这个问题的步骤

  1. 安装 MySQL 开发库
    • 在 Ubuntu/Debian 系统中,确保安装了 libmysqlclient-dev
      sudo apt-get install libmysqlclient-dev
    • 在 CentOS/RHEL 系统中,安装 mysql-devel
      sudo yum install mysql-devel

ERROR: Could not find a version that satisfies the requirement mysqlclient==2.2.1

环境信息

  • Ubuntu 22.04.4 LTS (Jammy Jellyfish)
  • Python 3.10

安装 mysqlclient==2.2.1 时报错:

Collecting mysqlclient==2.2.1
Using cached mysqlclient-2.2.1.tar.gz (89 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Installing backend dependencies ... done
Preparing metadata (pyproject.toml) ... done
WARNING: Generating metadata for package mysqlclient produced metadata for project name unknown. Fix your #egg=mysqlclient fragments.
Discarding https://files.pythonhosted.org/packages/37/fb/d9a8f763c84f1e789c027af0ffc7dbf94c9a38db961484f253f0552cbb47/mysqlclient-2.2.1.tar.gz#sha256=2c7ad15b87293b12fd44b47c46879ec95ec647f4567e866ccd70b8337584e9b2 (from https://pypi.org/simple/mysqlclient/) (requires-python:>=3.8): Requested unknown from https://files.pythonhosted.org/packages/37/fb/d9a8f763c84f1e789c027af0ffc7dbf94c9a38db961484f253f0552cbb47/mysqlclient-2.2.1.tar.gz#sha256=2c7ad15b87293b12fd44b47c46879ec95ec647f4567e866ccd70b8337584e9b2 (from -r requirements.txt (line 20)) has inconsistent name: filename has 'mysqlclient', but metadata has 'unknown'
ERROR: Could not find a version that satisfies the requirement mysqlclient==2.2.1 (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.1.0rc1, 2.1.0, 2.1.1, 2.2.0rc1, 2.2.0, 2.2.1, 2.2.2, 2.2.3, 2.2.4)
ERROR: No matching distribution found for mysqlclient==2.2.1

这个错误表明在尝试安装 mysqlclient==2.2.1 时,Python 包管理工具 pip 发现了元数据的不一致性,即文件名是 mysqlclient,但元数据标识的包名为 unknown。这可能是由于该版本的包在 PyPI 上发布时存在问题。

要解决这个问题,可以尝试使用不同的 mysqlclient 版本。例如不指定具体版本,默认安装最新的稳定版本,可以成功安装

There was a problem confirming the ssl certificate

环境信息

  • Python 2.7
  • Centos 7 6.8.9-1
# pip install shadowsocks
Collecting shadowsocks
Could not fetch URL https://pypi.python.org/simple/shadowsocks/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618) - skipping
Could not find a version that satisfies the requirement shadowsocks (from versions: )
No matching distribution found for shadowsocks

解决方法 1

pip install shadowsocks --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org