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

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
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
from mlagents.trainers.trainer_controller import TrainerController
File “f:\unity\ml-agents\ml-agents\mlagents\trainers\trainer_controller.py”, line 24, in
from mlagents.trainers.trainer_util import TrainerFactory
File “f:\unity\ml-agents\ml-agents\mlagents\trainers\trainer_util.py”, line 9, in
from mlagents.trainers.ppo.trainer import PPOTrainer
File “f:\unity\ml-agents\ml-agents\mlagents\trainers\ppo\trainer.py”, line 10, in
from mlagents.trainers.ppo.policy import PPOPolicy
File “f:\unity\ml-agents\ml-agents\mlagents\trainers\ppo\policy.py”, line 13, in
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
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
from mlagents.trainers.demo_loader import demo_to_buffer
File “f:\unity\ml-agents\ml-agents\mlagents\trainers\demo_loader.py”, line 12, in
from mlagents_envs.rpc_utils import (
File “f:\unity\ml-agents\ml-agents-envs\mlagents_envs\rpc_utils.py”, line 14, in
from PIL import Image
File “d:\programming\python\lib\site-packages\PIL\Image.py”, line 69, in
from . import _imaging as core
ImportError: cannot import name 'imaging’ from ‘PIL’ (d:\programming\python\lib\site-packages\PIL_init.py)

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: python - ImportError: cannot import name _imaging - Stack Overflow

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: ml-agents/docs/Using-Virtual-Environment.md at latest_release · Unity-Technologies/ml-agents · GitHub

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.

Cool, glad you got it sorted out.