# Python常见问题
# Could not find a version that satisfies the requirement setuptools_scm (from versions: none)
这个是setuptools-scm版本不匹配或不存在的问题,使用如下命令后再进行pip安装你需要的包就可以解决这个问题
pip install setuptools-scm
运行成功后再进行pip安装,即可安装成功
# The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored.
# 1. 单次使用
以阿里的源安装numpy为例:
pip install numpy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com
# 2. 修改pip.conf
在一般的Unix系统下,这个文件位于 ~/.pip/pip.conf
若没有.pip目录可以自行创建
在该文件中添加以下内容,然后保存退出即可
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
# 3. 命令修改pip.conf
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # 更改源
# 'builtin_function_or_method' object is not subscriptable 错误
大概率是因为括号用错了(比如应该用圆括号,用成了方括号),或者缺少括号,应检查括号是否使用有误。
# 无法加载文件/禁止运行脚本venv\Scripts\Activate.ps1 无法在powershell运行python venv
- WINDOWS+S搜索powershell并以管理员运行
- 输入set-executionpolicy remotesigned
- 输入Y
# 在 ubuntu/debian io.h 上使用 pip 时出现 Twisted-iocpsupport 错误
Building wheel for twisted-iocpsupport (PEP 517): started
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 /tmp/tmpv8s6qz76 build_wheel /tmp/tmp9l8hgcva
cwd: /tmp/pip-install-qsr4j4x_/twisted-iocpsupport
Complete output (13 lines):
running bdist_wheel
running build
running build_ext
building 'twisted_iocpsupport.iocpsupport' extension
creating build
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/twisted_iocpsupport
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Itwisted_iocpsupport -I/usr/include/pyth
on3.8 -c twisted_iocpsupport/iocpsupport.c -o build/temp.linux-x86_64-3.8/twisted_iocpsupport/iocpsupport.o
twisted_iocpsupport/iocpsupport.c:631:10: fatal error: io.h: No such file or directory
631 | #include "io.h"
| ^~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for twisted-iocpsupport
Building wheel for twisted-iocpsupport (PEP 517): finished with status 'error'
channels==3.0.3
daphne==3.0.1
Twisted==21.2.0
twisted-iocpsupport==1.0.1
在 docker 环境中:
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get -y install python3 python3-pip
WORKDIR /usr/src/app
# Copy requirements
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
解决:
删掉该依赖,重新安装
Twisted-iocpsupport 是一个包,提供与 Windows“I/O 完成端口”API 的绑定。是仅限 Windows 的 API,因此不能在 非windows系统上使用这个包。
# pip安装出现报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xc3 in position 4
使用pip命令安装模块时,若出现报错:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 4: invalid continuation byte
因为windows下命令行的代码页为GBK,但是程序编码是UTF-8
# 1. 修改python源码
找到python安装目录下\Lib\site-packages\pip\compat的__init__.py,文件中约75行:
将:return s.decode('utf_8') 修改为:
return s.decode("gbk")
# 2. 配置cmd编码
将Windows命令提示符中的编码修改为UTF-8
chcp 65001
# jupyter显示某包未安装/不存在
import pip
def install(package):
pip.main(['install', package])
install('imblearn')
或直接在jupyter执行下面代码
!pip install imblearn
# 安装jinja2:PermissionError: [Errno 13] Permission denied: 'f:\work\helloaiohttp\env\Lib\site-packages\aiohttp_helpers.cp36-win_amd64.pyd'
错误产生的原因是文件无法打开,可能产生的原因是文件找不到,或者被占用,或者无权限访问,或者打开的不是文件,而是一个目录。
解决方案如下:
- 检查对应路径下的文件是否存在,且被占用。如果文件不存在,就找到对应文件即可;如果文件存在,被占用,将占用程序暂时关闭。
- 修改cmd的权限,以管理员身份运行。
- 检查是否是打开了文件夹。
# pip install -e . :pip安装本地包
使用pip install -e . 安装时,pip会自动将包复制到site-packages目录下。
# pip安装包时无法安装进虚拟环境而安装在全局
# windows
让当前的执行策略优于计算机的执行策略
set-ExecutionPolicy RemoteSigned
activate
# python创建文件时文件名不全
例如r_220921_01:24.txt创建出来的文件只是r_220921_01,因为文件名中不能出现冒号:。
windows下文件命名规范:
- 文件名应该不超过260个字符,包括文件路径和文件名。
- 文件名只能包含字母、数字、下划线和连字符。
- 文件名不能包含以下字符:/ \ : * ? " < > |。
# import时 module not found
将包的路径加入sys.path变量中即可。
注意:Python解释器只会将入口文件或者说执行的第一个文件的路径(所在的文件夹)加入path中,所以有时会产生上面的问题。
解决方案参考如下:
# 获取文件/文件夹路径
# 获取所在文件夹路径 os.path.dirname(__file__) os.path.dirname(os.path.abspath(__file__)) # 获取文件绝对路径 os.getcwd()
# ImportError: cannot import name 'Feature' from 'setuptools'
python37在安装包时出现这个问题,更换setuptools的版本即可。
python -m pip install --upgrade pip setuptools==45.2.0
# SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip
升级setuptool即可。
python -m pip install --upgrade pip
# pip安装包时报错: fatal error C1083: 无法打开包括文件: “io.h”: No such file or directory
使用pip安装模块,出现错误:
c:\users\anaconda3\include\pyconfig.h(68): fatal error C1083: 无法打开包括文件: “io.h”: No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
----------------------------------------
Command "C:\Users\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\AppData\\Local\\Temp\\pip-build-4u1qzu
cg\\lmdb\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:
\Users\AppData\Local\Temp\pip-bcivjbyw-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in
C:\Users\AppData\Local\Temp\pip-build-4u1qzucg\lmdb\
无法找到io.h文件。
解决办法:安装Windows 10 SDK即可。
# pip安装包时报错:ModuleNotFoundError: No module named '_distutils_hack'
更新setuptools即可。
pip install setuptools==57.5.0
# pip安装包时报错:use_2to3 is invalid.
主要原因是在setuptools 58之后的版本已经废弃了use_2to3,只需安装setuptools 低于58版本就可以了。
pip install setuptools==57.5.0
# AttributeError: module ‘numpy‘ has no attribute ‘ndarray‘
原因是numpy与pandas的版本不匹配,卸载重装即可。注意需要先安装numpy,然后安装pandas。
# Pip ProxyError 、SSLError: Max retries exceeded with url
当开启代理时pip安装包报错:
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError(0, 'Error'))': /root/dev/loguru/
# 方案1:关闭代理
如果是linux环境可以使用以下命令,其中第一句为查看是否开启了代理,后面的都是关闭代理(实际上就是清除环境变量)。
env | grep -i proxy
unset https_proxy
unset http_proxy
unset ALL_PROXY
unset all_proxy
# 方案2:pip安装包时使用--trusted-host选项 - 推荐
意思应该是就不走SSL握手了(我猜的)
pip install keras -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
# 方案3:编辑pip.conf文件
本质上是将方案2写入配置文件 - ~/.pip/pip.conf
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
# 方案4:编辑pip.ini文件
本质是方案3的命令行配置版
pip config set global.index-url http://pypi.douban.com/simple
pip config set install.trusted-host pypi.douban.com
# conda创建环境时报错current_repodata.json失败
报错如下:Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
换用国内镜像源或开启代理,由于国内神奇的网络环境,出现这种问题一点也不奇怪。
# Microsoft Visual C++ 14.0 or greater is required
pip安装包时报上述错误
安装Microsoft Visual C++ 14.0 or greater即可。
- 进入 vs官网 (opens new window),下载安装
- 成功安装后,还要安装以下内容:
# required to install pyproject.toml-based projects
去PYPI官网下whl文件然后手动安装即可- pyproject-toml (opens new window)
# uWSGI:Exception: you need a C compiler to build uWSGI
换conda-forge源进行安装。
conda install -c conda-forge uwsgi
# jupyter打开文件:out of memory
有一个文件输出了过多内容导致了在浏览器使用jupyter打开这个文件时出现了这个问题,
解决办法:
- 直接编辑文件删除有过多输出的块。
- 使用vscode或pycharm利用插件可以正常打开文件。
# docker-swarm启动的项目:mysql连不上
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'czt-mysql' ([Errno 111] Connection refused)")
记一次MySQL容器不断重启
结论:环境出问题了,跟项目本身和mysql没有太大的关系。
# jupyter重置密码
执行命令
jupyter notebook password
# pip源配置不生效
理论上来说 pip install 命令会自动选择当前可用镜像源下载,但实际看来其选取判断存在缺陷,所以可能会出现某个源暂时不可用但还一直尝试通过该源进行下载的情况。
遇到这种情况建议直接使用-i的方式手动指定镜像源
# Python使用requests時遇到Failed to establish a new connection
解决方法:
程式邏輯的關係我會在短時間使用多次requests.post
其結果就是跳出了Failed to establish a new connection這樣一個錯誤
原因未知,但有一個根本的解決方法是在發起一個http request之後設定header將其关闭
requests..get("http://...", headers={'Connection':'close'})
requests..post("http://...", headers={'Connection':'close'})
aax1=requests.get(urla1,headers=headt)
requests.get(urla1,headers=headclose)
# Python代码中的分号;
Python可以在同一行中使用多条语句,语句之间使用分号(;)分割
#!/usr/bin/python3
import sys; x = 'nowcoder'; sys.stdout.write(x + '\n')
# 查看python解释器路径
import sys
print(sys.executable)
# pywin32报错:module ‘win32com.gen_py.00020905-0000-0000-C000-000000000046x0x8x7‘ has no attribute
许久没动的用pywin32写的生成word的项目报错了,而自己确定中间没有动过环境和代码,最终查了一下居然是需要删除缓存文件。
解决方法:删除目录C:\Users\Administrator\AppData\Local\Temp\gen_py\3.7中的缓存文件夹00020905-0000-0000-C000-000000000046x0x8x7即可,重新执行上述代码便不再报错。
参考原文地址:https://blog.csdn.net/Saladbobo/article/details/103381335 (opens new window)