1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pip uninstall時のUnicodeDecodeError対処法【venv環境】

Last updated at Posted at 2021-03-21

環境

Windows 10
Python 3.8.5
pip 20.1.1
venv環境

エラー内容

すでにインストールされていたnumpy 1.19.2を1.18.5にバージョンダウンしようとした時に,以下のUnicodeDecodeErrorが発生しました.
パスに日本語が含まれていたからでしょうか…?

(.venv) PS G:\研究\product> pip install numpy==1.18.5
Collecting numpy==1.18.5
  Using cached numpy-1.18.5-cp38-cp38-win_amd64.whl (12.8 MB)
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 1.19.2
ERROR: Exception:
Traceback (most recent call last):
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\cli\base_command.py", line 188, in _main
    status = self.run(options, args)
    return func(self, options, args)
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\commands\install.py", line 398, in run
    installed = install_given_reqs(
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\req\__init__.py", line 63, in install_given_reqs
    uninstalled_pathset = requirement.uninstall(
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\req\req_install.py", line 675, in uninstall
    uninstalled_pathset = UninstallPathSet.from_dist(dist)
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\req\req_uninstall.py", line 456, in from_dist
    if not dist_is_local(dist):
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\misc.py", line 383, in dist_is_local
    return is_local(dist_location(dist))
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\misc.py", line 529, in dist_location
    egg_link = egg_link_path(dist)
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\misc.py", line 505, in egg_link_path
    if not virtualenv_no_global() and user_site:
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\virtualenv.py", line 111, in virtualenv_no_global
    return _no_global_under_venv()
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\virtualenv.py", line 72, in _no_global_under_venv
    cfg_lines = _get_pyvenv_cfg_lines()
  File "g:\研究\product\.venv\lib\site-packages\pip\_internal\utils\virtualenv.py", line 55, in _get_pyvenv_cfg_lines
    return f.read().splitlines()  # avoids trailing newlines
UnicodeDecodeError: 'cp932' codec can't decode byte 0x88 in position 24: illegal multibyte sequence

解決方法

エラーの一番下にある,.venv\lib\site-packages\pip\_internal\utils\virtualenv.pyの55行目を探して,以下のように変更しました.
ちなみに.venvはvenvで設定した環境名です.

変更前

with open(pyvenv_cfg_file) as f:
    return f.read().splitlines()

変更後

with open(pyvenv_cfg_file, encoding="utf-8") as f:   # encodingを追加
    return f.read().splitlines()

結果

無事バージョンダウンできました.

(.venv) PS G:\研究\product> pip install numpy==1.18.5
Collecting numpy==1.18.5
  Using cached numpy-1.18.5-cp38-cp38-win_amd64.whl (12.8 MB)
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 1.19.2
    Uninstalling numpy-1.19.2:
      Successfully uninstalled numpy-1.19.2
Successfully installed numpy-1.18.5
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?