Error installing torchvision: cannot import PILLOW_VERSION from PIL

windows
Need to install pytorch and torchvision

Problem encountered (please include all error codes if any)
When trying to run in jupyter-lab, importing pytorch works fine but importing torchvision always raises an error
image

Attempted solutions
Tried many solutions found online with no effect; attempted multiple times to install using both conda and pip from the official website, downloads work fine in Anaconda Prompt but errors occur in jupyter-lab

b395266bf8b3b366d201e6152a6c4a8

Try uninstalling PIL with pip and reinstalling it to see if it works.

What does reinstalling PIL mean? I just uninstalled torch in the cmd.

Your error should be caused by import PILLOW_VERSION from PIL, so try first pip uninstall PIL to uninstall PIL, then pip install PIL to reinstall it.

Use this keyword to search on Bing
Installation of torchvision error can not import PILLOW_VERSION from PIL

Please provide all error messages sequentially from start to finish, using text instead of images whenever possible.

This makes it easier for others to help diagnose the problem.

For example:

(torch) C:\Users\123456>pip uninstall PIL
WARNING: Skipping PIL as it is not installed.

(torch) C:\Users\123456>pip install PIL
ERROR: Could not find a version that satisfies the requirement PIL (from versions: none)
ERROR: No matching distribution found for PIL

Try reinstalling Jupyter and PyTorch in a new virtual environment; this is the most convenient way :joy:

This means you want to uninstall PIL, but it was never installed before.

I want to install PIL, but the machine doesn’t know which version to install.

Okay, I’ll start over.

After switching to the new virtual environment

just enter this line to install everything

pip install jupyterlab torch torchvision torchaudio

This means installing jupyterlab, torch, torchvision, and torchaudio with pip.

There was an error, is it due to slow internet speed? Can I use the Tsinghua mirror?

ERROR: Exception:
Traceback (most recent call last):
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 437, in _error_catcher
    yield
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read
    data = self._fp_read(amt) if not fp_closed else b""
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 526, in _fp_read
    return self._fp.read(amt) if amt is not None else self._fp.read()
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 90, in read
    data = self.__fp.read(amt)
  File "C:\anocoda\envs\torch1\lib\http\client.py", line 465, in read
    n = self.readinto(b)
  File "C:\anocoda\envs\torch1\lib\http\client.py", line 509, in readinto
    n = self.fp.readinto(b)
  File "C:\anocoda\envs\torch1\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\anocoda\envs\torch1\lib\ssl.py", line 1071, in recv_into
    return self.read(nbytes, buffer)
  File "C:\anocoda\envs\torch1\lib\ssl.py", line 929, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
    status = run_func(*args)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\cli\req_command.py", line 247, in wrapper
    return func(self, options, args)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\commands\install.py", line 401, in run
    reqs, check_supported_wheels=not options.target_dir
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 93, in resolve
    collected.requirements, max_rounds=try_to_avoid_resolution_too_deep
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 481, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 348, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 155, in __bool__
    return any(self)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 211, in _make_candidate_from_link
    version=version,
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 303, in __init__
    version=version,
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 162, in __init__
    self.dist = self._prepare()
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 231, in _prepare
    dist = self._prepare_distribution()
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 308, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\operations\prepare.py", line 491, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\operations\prepare.py", line 542, in _prepare_linked_requirement
    hashes,
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\operations\prepare.py", line 170, in unpack_url
    hashes=hashes,
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\operations\prepare.py", line 107, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\network\download.py", line 147, in __call__
    for chunk in chunks:
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar
    for chunk in iterable:
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_internal\network\utils.py", line 87, in response_chunks
    decode_content=False,
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 621, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 586, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\anocoda\envs\torch1\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\anocoda\envs\torch1\lib\site-packages\pip\_vendor\urllib3\response.py", line 442, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

443 error, switch to the Tsinghua mirror

The new virtual environment I created is torch1. It seems to have been installed successfully on the Anaconda prompt, but it reports an error on jupyter-lab.

(base) C:\Users\123456>activate torch1
(torch1) C:\Users\123456>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision torchaudio
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: torch in c:\anocoda\envs\torch1\lib\site-packages (1.13.1)
Collecting torchvision
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/77/ec/ccdfcafb958f6007cc357ce12fd945551a71503b88cd3f78e49fd958f949/torchvision-0.14.1-cp37-cp37m-win_amd64.whl (1.1 MB)
Collecting torchaudio
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/01/66/0b5689b4a26255b23bf899d9ae016b04d9ea244e944b8010a71fe5f500ff/torchaudio-0.13.1-cp37-cp37m-win_amd64.whl (2.0 MB)
Requirement already satisfied: typing-extensions in c:\anocoda\envs\torch1\lib\site-packages (from torch) (4.7.1)
Collecting requests
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl (62 kB)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\anocoda\envs\torch1\lib\site-packages (from torchvision) (9.5.0)
Requirement already satisfied: numpy in c:\anocoda\envs\torch1\lib\site-packages (from torchvision) (1.21.6)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\anocoda\envs\torch1\lib\site-packages (from requests->torchvision) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in c:\anocoda\envs\torch1\lib\site-packages (from requests->torchvision) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\anocoda\envs\torch1\lib\site-packages (from requests->torchvision) (2022.12.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\anocoda\envs\torch1\lib\site-packages (from requests->torchvision) (2.0.4)
Installing collected packages: requests, torchvision, torchaudio
Successfully installed requests-2.31.0 torchaudio-0.13.1 torchvision-0.14.1

The error shown on jupyter-lab is modlenotfound.

This issue has also been reported by others. You can install it in Jupyter using pip. I guess it’s probably an environment variable problem, but that’s just a guess.
!pip install torch torchvision torchaudio

93854b3bea724298f0267f345498d9b8

It doesn’t seem to be this problem
image

The method of using !pip in Jupyter has been verified twice by myself and other classmates without any issues; at least this way it can be installed and used.

  • Install jupyter with pip in a fresh virtual environment
  • Use !pip to install torch inside Jupyter

If you want to know more about why such issues occur and how to solve them, you can search for the keywords related to this problem.

https://blog.csdn.net/denglavender/article/details/111707097

No errors now, but the result doesn’t seem to be what I wanted. It looks like it’s the CPU version.

image

1 Like