Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 python 库遇到的问题 #19

Open
richardmyu opened this issue Feb 15, 2021 · 8 comments
Open

使用 python 库遇到的问题 #19

richardmyu opened this issue Feb 15, 2021 · 8 comments
Labels
Projects

Comments

@richardmyu
Copy link
Owner

richardmyu commented Feb 15, 2021

1.time

使用 time 模块时,误输 time.strftime('%Y%m%D')

# 正确写法
print('--%Y%m%d--', time.strftime('%Y%m%d'))
# --%Y%m%d-- 20210215

# 误写
print('--%Y%m%D--', time.strftime('%Y%m%D'))
# --%Y%m%D-- 20210202/15/21

Python 参考手册.,可以用查到,没有明确指出 %D 的含义。暂时没弄明白,先记录一下这个结果。


print('--%D--', time.strftime('%D'))
# --%D-- 04/09/21

虽然手册没说,但通过实际测试发现,%D 表示美式日期。

@richardmyu richardmyu added this to Python in Language Feb 15, 2021
@richardmyu
Copy link
Owner Author

richardmyu commented Mar 2, 2021

2.cv2

这个库之前没有接触过,不过 demo 需要安装这个库,便就引入了这个库,然后安装这个库(pycharm 提示没有找到,按提示安装。。。)最后安装失败:

PackagesNotFoundError: The following packages are not available from current channels:

  - cv2

Current channels:

  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/noarch

这里需要补充一句,pycharm 之前用的解释器是单独下载的 python 3.8,后来看到了anaconda,便安装了anaconda,并将 pycharm 的解释器更换为 anaconda3 下的 python 3.8。

解决方法:这里面有一个隐藏问题,即安装库的名称应该是 opencv-python,而引入的时候要写出 import cv2,来源参考 Py之cv2:cv2库(OpenCV,opencv-python)的简介、安装、使用方法(常见函数、方法等)最强详细攻略

关于 opencv 和 cv2 名称问题,更多具体细节参考Python · cv2(零)


即使更换了源,安装速度还是特别感人,而且还会出现超时错误。。。

# 超时设置
pip --default-timeout=100 install package_name

顺便把 pip 的源也换了吧(早该换了。。。)

# 升级 pip 
pip install pip -U

# 确保 升级 pip 到最新的版本 (>=10.0.0) 后
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

这就完了。。。naive

pip list 查看,确实安装了 opencv-python 模块,但是 pycharm 还是提示找不到该模块,去 pycharm 的解释器设置中查看,发现并没有 opencv-python

其实是安装(opencv-python)的环境和 pycharm 中设置的解释器不是同一个环境。

解决 cv2 引入问题的直接办法,是在设置解释器的环境中,直接安装,但安装的不是 opencv-python,而是 opencv,不存在 opencv-python 这个包。

@richardmyu
Copy link
Owner Author

richardmyu commented Mar 2, 2021

3.anaconda

1. An HTTP error occurred when trying to retrieve this URL.

# wan
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/bottleneck-1.3.2-py37h2a96729_1.conda>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

解决方法:更换源

# anaconda prompt
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

不想使用命令行,可以编辑 C盘/用户/(你的用户名xx)/.condarc 文件,写入以下内容:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true

2. The current user does not have write permissions to the target environment.

EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
  environment location: C:\xx\Anaconda3

解决方法:(可能是因以管理员身份安装的导致的)调整文件权限,具体参考 安装TensorFlow遇到 The current user does not have write permissions to the target environment解决方案

3.HTTP 000 CONNECTION FAILED for url

今天在学习 turtle 绘图时,看来一下版本,发现比较老了...于是准备更新,在使用 conda 更新,报错了:

Collecting package metadata (current_repodata.json): -failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/current_repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64'

试了几次都没有成功,然后换了几次源,依旧没有成功过...直到看到这篇文章,我选择将源 URL 的 https 替换成 http,然后一下就成功了😂😂😂。遂记录一下。(HTTP errors are often intermittent, and a simple retry will get you on your way 就是多试几次)


一般的包查看版本,使用 xx.__version__,而 turtle 不行,看了一下源码:

# 103
_ver = "turtle 1.1b- - for Python 3.1   -  4. 5. 2009"

所以查看版本使用 turtle._ver

4.InvalidVersionSpecError

使用 anaconda prompt 更新:conda update anaconda-navigator,有如下报错:

Solving environment: failed

InvalidVersionSpecError: Invalid version spec: =2.7

环境参数:

 conda version : 4.5.11
conda-build version : 3.15.1
python version : 3.7.0.final.0

解决方法 1:

conda install --channel defaults conda python=3.6 --yes
conda update --channel defaults --all --yes
!conda --version

实测第一步无法成功,issue#10618 也有人如此反馈。

解决方法 2:进入 .condarc 文件,删除 channelsconda-forge 行,实测可以成功的,但是根据 issue#10618 讨论可知:1.有些包依赖 conda-forge,单纯删除一行,不是一个可以接受的方案。

解决方法 3:一种降低版本(有人指出 4.3.30 版本也有问题),一种提高版本(issue#198 bottle 猜测 InvalidVersionSpecError 是版本过低引起的,提问者升级后成功解决问题)。

升级(4.10.1)以后,确实没有这个问题了。但是,navigator 启动不了了...:rofl::rofl::rofl:


参考:

InvalidVersionSpecError: Invalid version spec: =2.7 #10618
Error in conda package installation #198

@richardmyu richardmyu changed the title 使用 python 库遇到的问题(合集) 使用 python 库遇到的问题(WIN10) Mar 2, 2021
@richardmyu richardmyu pinned this issue Mar 2, 2021
@richardmyu richardmyu unpinned this issue Mar 2, 2021
@richardmyu richardmyu reopened this Mar 2, 2021
@richardmyu
Copy link
Owner Author

richardmyu commented Jun 24, 2021

4.pip

4.1.There was a problem confirming the ssl certificate
python v3.8.8
pip v20.3.1

# pip.ini
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/

[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

使用 pip install imageio 时失败报错:

WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1125)'))': /simple/imageio/
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/imageio/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/imageio/ (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1125)'))) - skipping
ERROR: Could not find a version that satisfies the requirement imageio
ERROR: No matching distribution found for imageio
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1125)'))) - skipping

查证后发现是代理的锅(【#5053】开了代理使得 pip 难以验证 SSL 证书,所以需要使用 --trusted--host 来指定可信任的安装源,但注意,这不是最安全的解决方法。没说其他解决方法,更没有提到安全解决方案,不过既然知道是代理的问题,不用想也知道关闭代理再安装是比较安全的方法)。

即:在开启了代理的情况下,需要添加 --trusted-host pypi.xx.xx;然而这条对清华源不管用,但对豆瓣源管用;要想使用清华源,可以选择暂时关闭代理。(只测试过清华和豆瓣的源,其他未测未知)

为了流畅的访问 github,而使用了代理;而代理本身除了这次影响,还影响到微软商店的访问和下载,再有的负面影响就是不开代理,正常网页都被影响,关闭“使用代理服务器”以后就好了。

https://pypi.douban.com/simple
https://pypi.tuna.tsinghua.edu.cn/simple/

Could not fetch : There was a problem confirming the ssl certificate(SSL.c:3566) #5063

4.2.pip search

在解决安装问题后,搜索也出了问题。搜索带来两种报错:

# 第一种
xmlrpc.client.Fault: <Fault -32500: "RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future. See https://status.python.org/ for more information.">

# 第二种
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
...
pip._vendor.urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现 有的连接。', None, 10054, None))

第一个问题简单,看提示:由于无法管理的负载,PyPI 的 XMLRPC API 已被暂时禁用,并将在不久的将来弃用。

再看 网站,简单说就是 search 功能被滥用,官方停了 😞 😟 😠 😡 😪。

第二种报错在第一种报错后再来一次,基于第一种报错的解析,可知知道访问频繁,被当成恶意访问,强制关闭了链接 😭 🙄。


Remove the pip search command #5216

@richardmyu richardmyu changed the title 使用 python 库遇到的问题(WIN10) 使用 python 库遇到的问题 Nov 9, 2021
@richardmyu
Copy link
Owner Author

richardmyu commented Nov 9, 2021

5.python setup.py egg_info Check the logs for full command output

Ubuntu 20.04.1
python 3.8.10
pip3 20.0.2

安装 turtle 时报错:

ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kv40q4bf/turtle/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kv40q4bf/turtle/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-kv40q4bf/turtle/pip-egg-info
         cwd: /tmp/pip-install-kv40q4bf/turtle/
    Complete output (6 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-kv40q4bf/turtle/setup.py", line 40
        except ValueError, ve:
                         ^
    SyntaxError: invalid syntax
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

语法不兼容的问题。setup.py 是 python2 语法,当前环境为 python3。


最后发现系统自带 turtle,故不用下载。

@richardmyu
Copy link
Owner Author

6.No module named 'tkinter'

调用系统安装的 turtle 的报错。

安装 tk:

sudo apt install python3-tk

参考:

Python下"No module named _tkinter"问题解决过程总结
Python3下提示No module named 'tkinter'"问题解决

@richardmyu
Copy link
Owner Author

richardmyu commented May 26, 2022

7.引入包错误

  • cannot import name 'escape' from 'jinja2'

参考:[Solved] ImportError: cannot import name 'escape' from 'jinja2'

具体:

$ py app.py                      
Traceback (most recent call last):
  File "app.py", line 3, in <module>
    from flask import Flask, render_template, url_for, request, redirect
  File "/home/.../todo_app/venv/lib/python3.8/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/home/.../todo_app/venv/lib/python3.8/site-packages/jinja2/__init__.py)
(venv) 

分析:

jinja 升级版本移除了这个函数。

方案:

1.降级:jinja2<3.1.0
2.换包:from markupsafe import escape (jinja 中的 Markupescape 应该 Markusafe 引入);


  • cannot import name ‘json’ from itsdangerous

参考:[Solved] ImportError: cannot import name ‘json’ from itsdangerous

具体:

$ py app.py                      
Traceback (most recent call last):
  File "app.py", line 3, in <module>
    from flask import Flask, render_template, url_for, request, redirect
  File "/home/.../todo_app/venv/lib/python3.8/site-packages/flask/__init__.py", line 19, in <module>
    from . import json
  File "/home/.../todo_app/venv/lib/python3.8/site-packages/flask/json/__init__.py", line 15, in <module>
    from itsdangerous import json as _json
ImportError: cannot import name 'json' from 'itsdangerous' (/home/.../todo_app/venv/lib/python3.8/site-packages/itsdangerous/__init__.py)
(venv) 

分析:还是版本问题

方案:解决这个问题后,又出现了其他引入问题,索性将版本都升级到最新版本,成功运行。

@qixingyu-code
Copy link

你好,请问你遇到过如下错误吗?
在使用conda 命令安装package的时候,提示conda的版本过低,使用conda uodate conda 命令,但是出现问题
Solving environment: failed
InvalidVersionSpecError: Invalid version spec: =2.7
我应该如何顺利更新自己的conda 呢

@Lr-2002
Copy link

Lr-2002 commented Feb 6, 2024

@qixingyu-code 同问,请问朋友你解决了这个问题不

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Language
Python
Development

No branches or pull requests

3 participants