关于pycharm中pip版本10.0无法使用(pip升级失败)的终极解决办法

一、背景:

在使用pycharm(2018.3)创建python项目时,如果采用虚拟环境的方式,默认安装pip版本是10.0.1。执行pip list会提示

You are using pip version 10.0.1, however version 19.0.3 is   available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

但是,在虚拟环境中执行

python -m pip install --upgrade pip

时就会出现如下错误导致升级失败,而且虚拟环境中原先的pip10也被破坏了。错误内容如下:

Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
  Rolling back uninstall of pip
Exception:
Traceback (most recent call last):
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\basecommand.py", line 228, in main
    status = self.run(options, args)
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\commands\install.py", line 335, in run
    use_user_site=options.use_user_site,
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\req\__init__.py", line 49, in install_given_reqs
    **kwargs
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\req\req_install.py", line 748, in install
    use_user_site=use_user_site, pycompile=pycompile,
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\req\req_install.py", line 961, in move_wheel_files
    warn_script_location=warn_script_location,
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_internal\wheel.py", line 431, in move_wheel_files
    generated.extend(maker.make(spec))
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\distlib\scripts.py", line 403, in make
    self._make_script(entry, filenames, options=options)
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\distlib\scripts.py", line 307, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\distlib\scripts.py", line 243, in _write_script
    launcher = self._get_launcher('t')
  File "D:\appworks\pipdemo\venv\lib\site-packages\pip-10.0.1-py3.7.egg\pip\_vendor\distlib\scripts.py", line 382, in _get_launcher
    result = finder(distlib_package).find(name).bytes
AttributeError: 'NoneType' object has no attribute 'bytes'

二、问题:

虽然10.0.1版本的pip也可以安装第三方库,但是总是想升级到最新的版本上。另外,在网上查找解决方案时发现,10.0.1版本的pip在安装某些第三方库时会出现错误导致安装失败。那么该如何升级到最新版本的pip呢?

三、终极解决方案:

网上搜到的解决方法真的是五花八门,有修改源代码的:比如这篇。
有删除虚拟环境中的pip目录然后再用pycharm重新装的:比如这篇,然后看这篇文章的评论里有人照着做了可以了也有人仍然不行。
以上方法都属于剑走偏锋,无法保证100%没问题。
经过本人的摸索,现向各位提供一个100%能够成功的解决方案。
不管您是刚创建完项目,还是使用了一段时间已经安装了一些第三方库都可以成功将pip10.0.1这个瘸脚猫升级为最新的19.0.3。

第一步:制作当前虚拟环境下已经安装的第三方库的清单。

(venv) D:\appworks\pipdemo>pip freeze > requirements.txt

我创建了一个Flask演示项目,刚创建完后pycharm会给建好目录结构生成项目文件并安装必要的依赖库。

certifi==2018.11.29
chardet==3.0.4
Click==7.0
Flask==1.0.2
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.1
requests==2.21.0
urllib3==1.24.1
Werkzeug==0.14.1

第二步:

关闭pycharm并删除由pycharm创建的本项目虚拟环境文件夹 venv。

第三步:

在控制台项目文件目录下创建新的Python3.7虚拟环境。

D:\appworks\pipdemo>python.exe -m venv venv

第四步:

激活刚创建的虚拟环境,查看pip版本。

D:\appworks\pipdemo>venv\Scripts\activate.bat
(venv) D:\appworks\pipdemo>pip --version
pip 18.1 from d:\appworks\pipdemo\venv\lib\site-packages\pip (python 3.7)

已经变成18.1了。
这时候再升级pip就没有任何问题了。

(venv) D:\appworks\pipdemo>python -m pip install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 3.4MB/s
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
(venv) D:\appworks\pipdemo>pip --version
pip 19.0.3 from d:\appworks\pipdemo\venv\lib\site-packages\pip (python 3.7)

第五步:

恢复项目依赖库环境。

(venv) D:\appworks\pipdemo>pip install -r requirements.txt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting certifi==2018.11.29 (from -r requirements.txt (line 1))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/9f/e0/accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/certifi-2018.11.29-py2.py3-none-any.whl
Collecting chardet==3.0.4 (from -r requirements.txt (line 2))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting Click==7.0 (from -r requirements.txt (line 3))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
Collecting Flask==1.0.2 (from -r requirements.txt (line 4))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl
Collecting idna==2.8 (from -r requirements.txt (line 5))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Collecting itsdangerous==1.1.0 (from -r requirements.txt (line 6))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2==2.10 (from -r requirements.txt (line 7))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl
Collecting MarkupSafe==1.1.1 (from -r requirements.txt (line 8))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/65/c6/2399700d236d1dd681af8aebff1725558cddfd6e43d7a5184a675f4711f5/MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl
Collecting requests==2.21.0 (from -r requirements.txt (line 9))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl
Collecting urllib3==1.24.1 (from -r requirements.txt (line 10))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl
Collecting Werkzeug==0.14.1 (from -r requirements.txt (line 12))
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl
Installing collected packages: certifi, chardet, Click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask, idna, urllib3, requests, wechat
  Running setup.py install for wechat ... done
Successfully installed Click-7.0 Flask-1.0.2 Jinja2-2.10 MarkupSafe-1.1.1 Werkzeug-0.14.1 certifi-2018.11.29 chardet-3.0.4 idna-2.8 itsdangerous-1.1.0 requests-2.21.0 urllib3-1.24.1

这样一来,整个项目的虚拟环境就恢复如初了,而且不会有任何后遗症。
完美~

以上文章为本人原创,转载请注明出处,谢谢~

你可能感兴趣的:(关于pycharm中pip版本10.0无法使用(pip升级失败)的终极解决办法)