python -- 解决"pip install pandas"时遇到的几个小问题(内含解决pip升级问题)

 

【本篇小结】

(1)pip升级,如果超时,可以采用镜像源处理。

 python -m pip install --upgrade pip -ihttp://pypi.douban.com/simple --trusted-host pypi.douban.com

(2)Install pandas时,如果连接超时,可以通过设置延时加镜像源处理。

pip install pandas --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

 

正文请往下阅读

 

今天使用pandas进行数据读取,遇到几个小问题,记录一下,备忘~

由于用了部新电脑,需要重新install,所以输入以下命令

>> pip install pandas

结果报错,显示以下错误信息

Could not find a version that satisfies the requirement numpy>=1.21.0; python_version >= "3.10" (from pandas) (from versions: none) ERROR: No matching distribution found for numpy>=1.21.0; python_version >= "3.10" WARNING: You are using pip version 21.3.

 

看到warning提示,先检查一下当前的pip版本

>> pip show pip
Name: pip Version: 21.3.1

升级pip

>> python -m pip install --upgrade pip

Requirement already satisfied: pip in c:\users\pycharmprojects\pythonproject\venv\lib\site-packages (21.3.1)
Collecting pip
  Downloading pip-23.1.2-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 11 kB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.3.1
    Uninstalling pip-21.3.1:
      Successfully uninstalled pip-21.3.1
Successfully installed pip-23.1.2

 

----【番外篇插曲-begin】 -----

隔了几天,又换了部新电脑,这边电脑上面的pip版本比较低

> pip show pip
Name: pip Version: 9.0.1 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: python-virtualenv@groups.google.com License: MIT Location: e:\python_workspace\mywriter\venv\lib\site-packages\pip-9.0.1-py3.6.egg Requires: You are using pip version 9.0.1, however version 23.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

升级pip,直接输入“python -m pip install --upgrade pip”

> python -m pip install --upgrade pip
Collecting pip
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/a4/6d/6463d49a933f547439d6b5b98b4 6af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x043D68D0>, 'Connection to files.pythonhosted.org timed out. (connect timeo ut=15)')': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x043D6570>, 'Connection to files.pythonhosted.org timed out. (connect timeo ut=15)')': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x043D6930>, 'Connection to files.pythonhosted.org timed out. (connect timeo ut=15)')': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x043D6910>, 'Connection to files.pythonhosted.org timed out. (connect timeo ut=15)')': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (Caused by ConnectTi meoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x043D6990>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)'))

 

尝试通过延长时间

> python -m pip --default-timeout=800 install --upgrade pip
Collecting pip
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))': /packages/a4/6d/6463d49a933f5 47439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x04FBCAF0>: Failed to establish a new connection: [WinError 10060] 由于连接 方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x04FBCBD0>: Failed to establish a new connection: [WinError 10060] 由于连接 方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x04FBCA90>: Failed to establish a new connection: [WinError 10060] 由于连接 方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x04FBCC30>: Failed to establish a new connection: [WinError 10060] 由于连接 方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)': /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (Caused by NewConnec tionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x04FBCC50>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))

 

最后采用镜像源解决。(参考:https://www.likecs.com/show-203886769.html)

> python -m pip install --upgrade pip -ihttp://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking
in indexes: http://pypi.douban.com/simple Collecting pip Downloading http://pypi.doubanio.com/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB) |████████████████████████████████| 1.7MB 6.8MB/s Installing collected packages: pip Found existing installation: pip 19.3.1 Uninstalling pip-19.3.1: Successfully uninstalled pip-19.3.1 Successfully installed pip-21.3.1

-----【番外小插曲-end】-----

 

再次 install pandas

>> pip install pandas
ERROR: Could not find a version that satisfies the requirement pandas (from versions: none) ERROR: No matching distribution found for pandas

出现Error信息提示,在上网查找了下相关信息,可以通过延长时间,再加上国内镜像源的方法来尝试解决。(参考:https://blog.csdn.net/weixin_42571952/article/details/102988289

>> pip install pandas --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pandas
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/90/30/8b857447b0f4b59d5bd84e934e82ef8c82b73d71d1c9611c8aaaa8d44a50/pandas-2.0.1-cp310-cp310-win_amd64.whl (10.7 MB)
     ---------------------------------------- 10.7/10.7 MB 3.3 MB/s eta 0:00:00
Collecting python-dateutil>=2.8.2 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ---------------------------------------- 247.7/247.7 kB 2.6 MB/s eta 0:00:00
Collecting pytz>=2020.1 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/99/ad6bd37e748257dd70d6f85d916cafe79c0b0f5e2e95b11f7fbc82bf3110/pytz-2023.3-py2.py3-none-any.whl (502 kB)
     ---------------------------------------- 502.3/502.3 kB 2.9 MB/s eta 0:00:00
Requirement already satisfied: tzdata>=2022.1 in c:\users\pycharmprojects\pythonproject\venv\lib\site-packages (from pandas) (2023.3)
Collecting numpy>=1.21.0 (from pandas)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/65/5d/46da284b0bf6cfbf04082c3c5e84399664d69e41c11a33587ad49b0c64e5/numpy-1.24.3-cp310-cp310-win_amd64.whl (14.8 MB)
     ---------------------------------------- 14.8/14.8 MB 4.1 MB/s eta 0:00:00
Requirement already satisfied: six>=1.5 in c:\users\pycharmprojects\pythonproject\venv\lib\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Installing collected packages: pytz, python-dateutil, numpy, pandas
Successfully installed numpy-1.24.3 pandas-2.0.1 python-dateutil-2.8.2 pytz-2023.3

接着我们通过命令查一下已安装pandas的版本

>> pip show pandas

Name: pandas
Version: 2.0.1

可见已经安装成功。

 

下面进行一个简单的读取操作,python代码如下:(备注:文件c_student.csv里的测试数据带有中文。)

import pandas as pd

data = pd.read_csv('e:/python_data_space/c_student.csv')
print(data)

提示错误信息:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 20: invalid continuation byte

修改下代码,补充encoding设置

import pandas as pd

data = pd.read_csv('e:/python_data_space/c_student.csv', encoding='gbk')
print(data)

查看结果

   id  name  score
0   1  小明   78.0
1   2  小红   87.0
2   3  小兰   99.0
3   4  小白   99.0

Process finished with exit code 0

 

此篇到此结束~

 

posted @ 2023-05-10 00:40  lmei  阅读(6395)  评论(0编辑  收藏  举报