Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Error importing '_imaging' from PIL when trying to use mlagents-learn

Discussion in 'ML-Agents' started by TheGreatBritishNinja, Feb 22, 2020.

  1. TheGreatBritishNinja

    TheGreatBritishNinja

    Joined:
    May 20, 2019
    Posts:
    3
    I installed and set up ml-agents a few days ago, managed to test it with the 3DBall example and everything was working fine. Yesterday I tried making my own project, but now every time I try and run mlagents-learn I keep getting this error:
    ImportError: cannot import name '_imaging' from 'PIL' (d:\programming\python\lib\site-packages\PIL\__init__.py)

    I tried reinstalling ml-agents, python and any of the affected modules I could think of, but nothing I've tried has worked so far, so I'm kind of stumped. Any advice?

    I'm using python 3.7.6, pip 20.0.2 and the latest version of ml-agents from the latest_release branch. Here's the full error:

    Traceback (most recent call last):
    File "D:\Programming\Python\Scripts\mlagents-learn-script.py", line 11, in <module>
    load_entry_point('mlagents', 'console_scripts', 'mlagents-learn')()
    File "d:\programming\python\lib\site-packages\pkg_resources\__init__.py", line 490, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
    File "d:\programming\python\lib\site-packages\pkg_resources\__init__.py", line 2854, in load_entry_point
    return ep.load()
    File "d:\programming\python\lib\site-packages\pkg_resources\__init__.py", line 2445, in load
    return self.resolve()
    File "d:\programming\python\lib\site-packages\pkg_resources\__init__.py", line 2451, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\learn.py", line 16, in <module>
    from mlagents.trainers.trainer_controller import TrainerController
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\trainer_controller.py", line 24, in <module>
    from mlagents.trainers.trainer_util import TrainerFactory
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\trainer_util.py", line 9, in <module>
    from mlagents.trainers.ppo.trainer import PPOTrainer
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\ppo\trainer.py", line 10, in <module>
    from mlagents.trainers.ppo.policy import PPOPolicy
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\ppo\policy.py", line 13, in <module>
    from mlagents.trainers.components.reward_signals.reward_signal_factory import (
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\components\reward_signals\reward_signal_factory.py", line 9, in <module>
    from mlagents.trainers.components.reward_signals.gail.signal import GAILRewardSignal
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\components\reward_signals\gail\signal.py", line 10, in <module>
    from mlagents.trainers.demo_loader import demo_to_buffer
    File "f:\unity\ml-agents\ml-agents\mlagents\trainers\demo_loader.py", line 12, in <module>
    from mlagents_envs.rpc_utils import (
    File "f:\unity\ml-agents\ml-agents-envs\mlagents_envs\rpc_utils.py", line 14, in <module>
    from PIL import Image
    File "d:\programming\python\lib\site-packages\PIL\Image.py", line 69, in <module>
    from . import _imaging as core
    ImportError: cannot import name '_imaging' from 'PIL' (d:\programming\python\lib\site-packages\PIL\__init__.py)
     
  2. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    Hi,
    This sounds like a problem in the PIL/Pillow libraries, so it's not directly related to ML-Agents.

    You might try some of the solutions recommended here: https://stackoverflow.com/questions/25340698/importerror-cannot-import-name-imaging

    In general (not just for ML-Agents), I strongly recommend using a virtual environment like venv to install you python packages; that way packages versions for one project don't interact with others. From the filenames in the callstack it sounds like you have PIL installed in the "global" python packages, which might be causing the bad interaction with Pillow. More information here: https://github.com/Unity-Technologi...est_release/docs/Using-Virtual-Environment.md
     
  3. TheGreatBritishNinja

    TheGreatBritishNinja

    Joined:
    May 20, 2019
    Posts:
    3
    Thanks, I managed to figure out the issue. I actually was using a virtual environment for this project, but for some reason I was still using the main path for pip, so everything was still being installed and run on my main dependencies file. I made a new venv and set up pip correctly, and now all the dependencies are installed exactly as they need to be.
     
  4. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    Cool, glad you got it sorted out.