安装python模块常见错误
-
matplotlib报错_tkinter.TclError: no display name and no $DISPLAY environment variable
1 2 3 |
import matplotlib import matplotlib.pyplot as plt fig=plt.figure() |
#交互式测试,此时报错
解决办法,在引用后添加下面这一行
1 |
matplotlib.use('Agg') |
例如:
1 2 3 4 |
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt fig=plt.figure() |
- 报错:
c/_cffi_backend.c:15:10: fatal error: ffi.h: 没有那个文件或目录
#include <ffi.h>
^~~~~~~
compilation terminated.
error: command ‘gcc’ failed with exit status 1
解决:
1 2 3 |
yum install -y libffi-devel python-devel 或ubuntu中 apt-get install -y libffi-dev python-dev libevent-dev |
- 报错:build/temp.linux-x86_64-3.4/_openssl.c:423:30: fatal error: openssl/opensslv.h: No such file or directory
1 |
apt-get install libssl-dev |
-
安装MySQL-python时报错ModuleNotFoundError: No module named ‘ConfigParser’
用命令easy_install MySQL-python安装时报如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
(env_py3) [root@vm2 assembly-line-migrate ]$ easy_install MySQL-python Searching for MySQL-python Reading https://pypi.org/simple/MySQL-python/ Downloading https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip#sha256=811040b647e5d5686f84db415efd697e6250008b112b6909ba77ac059e140c74 Best match: MySQL-python 1.2.5 Processing MySQL-python-1.2.5.zip Writing /tmp/easy_install-rljhsh3e/MySQL-python-1.2.5/setup.cfg Running MySQL-python-1.2.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-rljhsh3e/MySQL-python-1.2.5/egg-dist-tmp-h43s7pl1 Traceback (most recent call last): File "/root/.virtualenvs/env_py3/lib/python3.7/site-packages/setuptools/sandbox.py", line 154, in save_modules yield saved File "/root/.virtualenvs/env_py3/lib/python3.7/site-packages/setuptools/sandbox.py", line 195, in setup_context yield File "/root/.virtualenvs/env_py3/lib/python3.7/site-packages/setuptools/sandbox.py", line 250, in run_setup _execfile(setup_script, ns) File "/root/.virtualenvs/env_py3/lib/python3.7/site-packages/setuptools/sandbox.py", line 45, in _execfile exec(code, globals, locals) File "/tmp/easy_install-rljhsh3e/MySQL-python-1.2.5/setup.py", line 13, in <module> File "/tmp/easy_install-rljhsh3e/MySQL-python-1.2.5/setup_posix.py", line 2, in <module> ModuleNotFoundError: No module named 'ConfigParser' |
原因:
在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
解决方法:
找到文件 configparser ,更名为旧版本的名称 ConfigParser
1 |
cp /usr/local/python3.7/lib/python3.7/configparser.py /usr/local/python3.7/lib/python3.7/ConfigParser.py |
如果使用过程中出问题, 可以:
修改six模块为
1 2 3 4 |
try: import configparser except: from six.moves import configparser |
- Could not find a version that satisfies the requirement twisted[tls]>=18.7
解决:pip install https://github.com/twisted/twisted/archive/twisted-18.7.0.zip
- No module named _sqlite3
解决:yum install sqlite-devel
./configure –prefix=/root/.python_env –enable-loadable-sqlite-extensions && make && make install
- import MySQLdb as Database
ModuleNotFoundError: No module named ‘MySQLdb’
解决: pip install PyMySQL mysqlclient
- 用python3.7-alpine镜像构建安装gevent时报错:
alpine /usr/local/include/python3.7m/Python.h:11:10: fatal error: limits.h: No such file or directory
解决:
1 2 3 |
apk add --no-cache tzdata nginx git supervisor mariadb-client curl \ openldap-dev mariadb-dev openssl-dev musl-dev python3-dev \ redis libffi-dev gcc make bash build-base libev \ |
重点须安装: build-base libev, 然后再次安装即可成功.
另: 有篇文章: https://o0o0.club/%E5%9C%A8alpine%E4%B8%AD%E5%AE%89%E8%A3%85gevent%E7%9A%84%E9%AA%9A%E6%93%8D%E4%BD%9C/
即打包所有的:
1 2 |
cd /root/.python_env/.virtualenvs/venv/lib/python3.9/site-packages/ tar -czvf gevent_alpine_lib.tar.gz gevent gevent-20.12.1.dist-info greenlet-1.0.0.dist-info greenlet zope zope.event-4.5.0.dist-info zope.event-4.5.0-py3.6-nspkg.pth zope.interface-5.2.0.dist-info zope.interface-5.2.0-py3.9-nspkg.pth |
然后再添加Dockerfile
1 2 3 |
RUN set -x \ && pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r /opt/webhook/requirements.txt \ && tar zxvf /opt/webhook/gevent_alpine_lib.tar.gz -C /usr/local/lib/python3.7/site-packages/ |
这个我没有测能否用
- 安装python-ldap :Modules/constants.h:7:18: fatal error: lber.h: No such file or directory
解决: yum install -y openldap-devel
- pip install 时遇到:
INFO: pip is looking at multiple versions of asn1crypto to determine which version is compatible with other requirements. This could take a while.
INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. If you want to ato do so. To improve how pip performs, tell us what happened here: https://pip.pypa.io/surveys/backtracking
解决:加上–use-deprecated=legacy-resolve参数:
pip install -r requirements.txt –use-deprecated=legacy-resolve
- 在alpine中build时,pip安装软件时报错:
error: Can not find Rust compiler Failed building wheel for cryptography
解决: 增加ENV变量(You can skip the rust installation and other related dependencies by adding the line below before apk add commands):
- 安装pip install Mysql-Python时报错: error: ‘MYSQL’ {aka ‘struct st_mysql’} has no member named ‘reconnect’
解决:
In alpine you easily execute his work around with
sed '/st_mysql_options options;/a unsigned int reconnect;' /usr/include/mysql/mysql.h -i.bkp
or in your docker file obviously
RUN sed '/st_mysql_options options;/a unsigned int reconnect;' /usr/include/mysql/mysql.h -i.bkp
下面是我的Dockerfile部分:
1 2 3 4 5 6 7 8 9 10 |
&& apk add --virtual .python-rundeps $runDeps \ && sed '/st_mysql_options options;/a unsigned int reconnect;' /usr/include/mysql/mysql.h -i.bkp \ && mkdir -p /lib64 \ && ln -sf /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 \ && wget -O /etc/pip.conf $BASE_URL/python/pip.conf \ && pip install --no-cache-dir --upgrade pip \ {% if version.python != 2.7 %} && cp /usr/local/lib/python{{ version.python }}/configparser.py /usr/local/lib/python{{ version.python }}/ConfigParser.py \ {% endif %} && pip install --no-cache-dir --upgrade MySQL-python \ |
参考:https://github.com/DefectDojo/django-DefectDojo/issues/407
- 安装pip install pycurl报错: Could not run curl-config: [Errno 2] No such file or directory
解决:apk add curl-dev
参考:https://stackoverflow.com/questions/23937933/could-not-run-curl-config-errno-2-no-such-file-or-directory-when-installing
用python3 In Alpine linux you should do:
apk add curl-dev python3-dev libressl-dev
如果使用python2, 只需要安装apk add curl-dev
0 Comments