python 的版本要一致
如果你不想重建虚拟环境,但系统的 Python 已经更新了,有一种可能的解决方法是直接修复虚拟环境中的 Python 解释器路径。请注意,这种方法可能并不总是有效,取决于你的虚拟环境和操作系统的具体情况,但是它可以作为一种尝试。
对于使用 virtualenv
创建的环境,你可以尝试以下步骤:
找到虚拟环境中的 Python 执行文件:
对于 Windows,虚拟环境的 Python 执行文件通常在 ENV_NAME\Scripts\
目录下。
对于 Unix-like 系统(Linux, macOS),它通常在 ENV_NAME/bin/
目录中。
更新符号链接(仅限 Unix-like 系统):
如果你的操作系统支持符号链接(Linux 或 macOS),可以尝试更新到新的 Python 解释器。你可以删除旧的符号链接并创建一个指向新的 Python 解释器的链接:
rm /path/to/ENV_NAME/bin/python
ln -s /path/to/new/python /path/to/ENV_NAME/bin/python
替换 /path/to/new/python
为你新的 Python 解释器的路径,/path/to/ENV_NAME
是你的虚拟环境的路径。
请注意,这个方法对于 Windows 不适用,因为 Windows 使用可执行文件而非符号链接。而是要用新建的虚拟环境的 文件去替换,不替换可能会报错
修复 activate
脚本:
无论是 Windows 还是 Unix-like 系统,你都需要检查虚拟环境的激活脚本。在 Unix-like 系统中,它位于 ENV_NAME/bin/activate
,在 Windows 中,它位于 ENV_NAME\Scripts\activate.bat
。确保该脚本中设置的环境变量 VIRTUAL_ENV
和任何其他引用到旧 Python 解释器的路径都更新为新的路径。
这些步骤需要你有足够的权限来修改文件和创建符号链接,并且你应该确保了解每个步骤的作用。在进行这些修改之前,创建虚拟环境的备份可能是一个好主意。
如果上述方法不起作用,或者你觉得这些步骤过于复杂,重建虚拟环境可能是更安全、更简单的方法。
如果 pip list (pip list --no-cache-dir 去掉缓存)显示的包列表不对,请按以下步骤操作,确保 cd 到虚拟环境的 Scripts 目录执行:
(work_py) H:\py_envs\work_py>pip list
Package Version
------------------ --------
async-timeout 4.0.2
certifi 2024.2.2
charset-normalizer 2.0.12
hiredis 2.0.0
idna 3.6
importlib-metadata 4.8.3
packaging 21.3
pip 21.3.1
pyparsing 3.1.1
redis 4.3.6
rediscluster 0.2.0
requests 2.27.1
setuptools 59.6.0
typing_extensions 4.1.1
urllib3 1.26.18
wheel 0.37.1
zipp 3.6.0
(work_py) H:\py_envs\work_py>cd Scripts
(work_py) H:\py_envs\work_py\Scripts>whereis python
H:\py_envs\work_py\Scripts\python.exe
(work_py) H:\py_envs\work_py\Scripts>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()
['H:\\py_envs\\work_py', 'H:\\py_envs\\work_py\\lib\\site-packages']
>>> exit()
(work_py) H:\py_envs\work_py\Scripts>python -m pip uninstall pip setuptools
Found existing installation: pip 21.3.1
Uninstalling pip-21.3.1:
Would remove:
h:\py_envs\work_py\lib\site-packages\pip-21.3.1.dist-info\*
h:\py_envs\work_py\lib\site-packages\pip-21.3.1.virtualenv
h:\py_envs\work_py\lib\site-packages\pip\*
h:\py_envs\work_py\scripts\pip-3.6.exe
h:\py_envs\work_py\scripts\pip.exe
h:\py_envs\work_py\scripts\pip3.6.exe
h:\py_envs\work_py\scripts\pip3.exe
Proceed (Y/n)? y
Successfully uninstalled pip-21.3.1
Found existing installation: setuptools 59.6.0
Uninstalling setuptools-59.6.0:
Would remove:
h:\py_envs\work_py\lib\site-packages\_distutils_hack\*
h:\py_envs\work_py\lib\site-packages\distutils-precedence.pth
h:\py_envs\work_py\lib\site-packages\pkg_resources\*
h:\py_envs\work_py\lib\site-packages\setuptools-59.6.0.dist-info\*
h:\py_envs\work_py\lib\site-packages\setuptools-59.6.0.virtualenv
h:\py_envs\work_py\lib\site-packages\setuptools\*
Proceed (Y/n)? y
Successfully uninstalled setuptools-59.6.0
(work_py) H:\py_envs\work_py\Scripts>pip list
Package Version
--------------------- ----------
certifi 2024.2.2
charset-normalizer 2.0.12
distlib 0.3.8
filelock 3.4.1
html5lib 1.1
idna 3.6
importlib-metadata 4.8.3
importlib-resources 5.4.0
Pillow 8.4.0
pip 21.3.1
platformdirs 2.4.0
publicsuffix2 2.20191221
pypiwin32 223
pywin32 305
redis 3.5.3
redis-py-cluster 2.1.3
requests 2.27.1
setuptools 40.6.2
six 1.16.0
typing_extensions 4.1.1
urllib3 1.26.18
virtualenv 20.17.1
virtualenvwrapper-win 1.2.7
webencodings 0.5.1
zipp 3.6.0
(work_py) H:\py_envs\work_py\Scripts>pip install requests
Requirement already satisfied: requests in d:\program files\python36\lib\site-packages (2.27.1)
Requirement already satisfied: idna<4,>=2.5 in d:\program files\python36\lib\site-packages (from requests) (3.6)
Requirement already satisfied: certifi>=2017.4.17 in d:\program files\python36\lib\site-packages (from requests) (2024.2.2)
Requirement already satisfied: charset-normalizer~=2.0.0 in d:\program files\python36\lib\site-packages (from requests) (2.0.12)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\program files\python36\lib\site-packages (from requests) (1.26.18)
(work_py) H:\py_envs\work_py\Scripts>pip3 list
Package Version
--------------------- ----------
certifi 2024.2.2
charset-normalizer 2.0.12
distlib 0.3.8
filelock 3.4.1
html5lib 1.1
idna 3.6
importlib-metadata 4.8.3
importlib-resources 5.4.0
Pillow 8.4.0
pip 21.3.1
platformdirs 2.4.0
publicsuffix2 2.20191221
pypiwin32 223
pywin32 305
redis 3.5.3
redis-py-cluster 2.1.3
requests 2.27.1
setuptools 40.6.2
six 1.16.0
typing_extensions 4.1.1
urllib3 1.26.18
virtualenv 20.17.1
virtualenvwrapper-win 1.2.7
webencodings 0.5.1
zipp 3.6.0
(work_py) H:\py_envs\work_py\Scripts>python -m ensurepip
Looking in links: C:\Users\Administrator\AppData\Local\Temp\tmpndukg1lg
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
(work_py) H:\py_envs\work_py\Scripts>pip3 list
Package Version
------------------------------ ------------
aiocontextvars 0.2.2
astor 0.8.1
async-timeout 4.0.2
Babel 2.11.0
backports.zoneinfo 0.2.1
bcrypt 4.0.1
bottle 0.12.25
certifi 2022.6.15
cffi 1.15.1
charset-normalizer 2.0.12
click 8.0.4
colorama 0.4.5
coloredlogs 15.0.1
comtypes 1.2.0
contextvars 2.4
cryptography 39.0.2
cycler 0.11.0
dataclasses 0.8
ddddocr 1.4.7
decorator 4.4.2
Deprecated 1.2.13
dill 0.3.4
docx2pdf 0.1.8
et-xmlfile 1.1.0
Faker 14.2.1
Flask 2.0.3
flatbuffers 23.1.21
future 0.18.3
humanfriendly 10.0
idna 3.3
imageio 2.15.0
immutables 0.19
importlib-metadata 4.8.3
importlib-resources 5.4.0
itsdangerous 2.0.1
jieba 0.42.1
Jinja2 3.0.3
keras 2.10.0
kiwisolver 1.3.1
loguru 0.7.0
lxml 4.9.3
MarkupSafe 2.0.1
matplotlib 3.3.4
netifaces 0.11.0
networkx 2.5.1
numpy 1.19.5
onnxruntime 1.10.0
openai 0.8.0
opencv-python 4.7.0.68
opencv-python-headless 3.4.18.65
openpyxl 3.1.2
opt-einsum 3.3.0
packaging 21.3
paddle-bfloat 0.1.7
paddlepaddle 2.4.2
pandas 1.1.5
paramiko 3.0.0
pdfkit 1.0.0
Pillow 8.4.0
pip 18.1
protobuf 3.19.6
pyasn1 0.4.8
pycparser 2.21
pycryptodome 3.15.0
pygame 2.5.2
PyMySQL 1.0.2
PyNaCl 1.5.0
pyparsing 3.0.9
pypiwin32 223
pypng 0.20220715.0
PyQRCode 1.2.1
pyreadline 2.1
python-dateutil 2.8.2
python-docx 0.8.11
python-magic-bin 0.4.14
python-pptx 0.6.22
pytz 2022.7.1
pytz-deprecation-shim 0.1.0.post0
PyWavelets 1.1.1
pywin32 305
redis 4.3.4
requests 2.27.1
requests-toolbelt 0.10.1
rsa 4.9
schedule 1.1.0
scikit-image 0.17.2
scipy 1.5.4
selenium 3.141.0
setuptools 40.6.2
Shapely 1.8.5.post1
six 1.16.0
slack-bolt 1.18.0
slack-sdk 3.23.0
soupsieve 2.3.2.post1
tencentcloud-sdk-python-common 3.0.998
tencentcloud-sdk-python-tbp 3.0.998
termcolor 1.1.0
tifffile 2020.9.3
tqdm 4.64.1
typing-extensions 4.1.1
tzdata 2023.3
tzlocal 4.2
uiautomation 2.0.18
urllib3 1.26.12
Werkzeug 2.0.3
WeRoBot 1.13.1
wheel 0.37.1
win32-setctime 1.1.0
wrapt 1.14.1
xlrd 1.2.0
XlsxWriter 3.1.2
xlwt 1.3.0
xmltodict 0.13.0
zipp 3.6.0
You are using pip version 18.1, however version 21.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(work_py) H:\py_envs\work_py\Scripts>python -m pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-21.3.1
(work_py) H:\py_envs\work_py\Scripts>pip list
Package Version
------------------------------ ------------
aiocontextvars 0.2.2
astor 0.8.1
async-timeout 4.0.2
Babel 2.11.0
backports.zoneinfo 0.2.1
bcrypt 4.0.1
bottle 0.12.25
certifi 2022.6.15
cffi 1.15.1
charset-normalizer 2.0.12
click 8.0.4
colorama 0.4.5
coloredlogs 15.0.1
comtypes 1.2.0
contextvars 2.4
cryptography 39.0.2
cycler 0.11.0
dataclasses 0.8
ddddocr 1.4.7
decorator 4.4.2
Deprecated 1.2.13
dill 0.3.4
docx2pdf 0.1.8
et-xmlfile 1.1.0
Faker 14.2.1
Flask 2.0.3
flatbuffers 23.1.21
future 0.18.3
humanfriendly 10.0
idna 3.3
imageio 2.15.0
immutables 0.19
importlib-metadata 4.8.3
importlib-resources 5.4.0
itsdangerous 2.0.1
jieba 0.42.1
Jinja2 3.0.3
keras 2.10.0
kiwisolver 1.3.1
loguru 0.7.0
lxml 4.9.3
MarkupSafe 2.0.1
matplotlib 3.3.4
netifaces 0.11.0
networkx 2.5.1
numpy 1.19.5
onnxruntime 1.10.0
openai 0.8.0
opencv-python 4.7.0.68
opencv-python-headless 3.4.18.65
openpyxl 3.1.2
opt-einsum 3.3.0
packaging 21.3
paddle-bfloat 0.1.7
paddlepaddle 2.4.2
pandas 1.1.5
paramiko 3.0.0
pdfkit 1.0.0
Pillow 8.4.0
pip 21.3.1
protobuf 3.19.6
pyasn1 0.4.8
pycparser 2.21
pycryptodome 3.15.0
pygame 2.5.2
PyMySQL 1.0.2
PyNaCl 1.5.0
pyparsing 3.0.9
pypiwin32 223
pypng 0.20220715.0
PyQRCode 1.2.1
pyreadline 2.1
python-dateutil 2.8.2
python-docx 0.8.11
python-magic-bin 0.4.14
python-pptx 0.6.22
pytz 2022.7.1
pytz-deprecation-shim 0.1.0.post0
PyWavelets 1.1.1
pywin32 305
redis 4.3.4
requests 2.27.1
requests-toolbelt 0.10.1
rsa 4.9
schedule 1.1.0
scikit-image 0.17.2
scipy 1.5.4
selenium 3.141.0
setuptools 40.6.2
Shapely 1.8.5.post1
six 1.16.0
slack-bolt 1.18.0
slack-sdk 3.23.0
soupsieve 2.3.2.post1
tencentcloud-sdk-python-common 3.0.998
tencentcloud-sdk-python-tbp 3.0.998
termcolor 1.1.0
tifffile 2020.9.3
tqdm 4.64.1
typing_extensions 4.1.1
tzdata 2023.3
tzlocal 4.2
uiautomation 2.0.18
urllib3 1.26.12
Werkzeug 2.0.3
WeRoBot 1.13.1
wheel 0.37.1
win32-setctime 1.1.0
wrapt 1.14.1
xlrd 1.2.0
XlsxWriter 3.1.2
xlwt 1.3.0
xmltodict 0.13.0
zipp 3.6.0
(work_py) H:\py_envs\work_py\Scripts>