I am trying to use the simglucose package with OpenAI gym. I am encountering a strange problem; when I run the code below in a certain directory (let's call it problem_dir/
), it fails with the error below. However, when I copy-paste the file to other locations, it runs fine (I tried about 10 other locations, including child and parent directories of problem_dir/
). What could cause an error like the one below, but only when the Python file is in problem_dir/
?
Problematic code:
import gym
from gym.envs.registration import register
register(
id='simglucose-adolescent2-v0',
entry_point='simglucose.envs:T1DSimEnv',
kwargs={'patient_name': 'adolescent#002'}
)
env = gym.make('simglucose-adolescent2-v0') # ERROR HERE
The error:
(seldonian_library_env) james@james-desktop2019-Ub18:~$ python seldonian_library_repos/Engine/seldonian/RL/environments/temp.py
/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py:17: PkgResourcesDeprecationWarning: Parameters to load are deprecated. Call .resolve and .require separately.
result = entry_point.load(False)
Traceback (most recent call last):
File "seldonian_library_repos/Engine/seldonian/RL/environments/temp.py", line 8, in <module>
env = gym.make('simglucose-adolescent2-v0')
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 164, in make
return registry.make(id)
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 122, in make
env = spec.make()
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 88, in make
cls = load(self._entry_point)
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 17, in load
result = entry_point.load(False)
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2450, in load
return self.resolve()
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2456, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/home/james/seldonian_library_repos/Engine/seldonian/RL/environments/simglucose.py", line 62, in <module>
register(
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 161, in register
return registry.register(id, **kwargs)
File "/home/james/anaconda3/envs/seldonian_library_env/lib/python3.8/site-packages/gym/envs/registration.py", line 154, in register
raise error.Error('Cannot re-register id: {}'.format(id))
gym.error.Error: Cannot re-register id: simglucose-adolescent2-v0
I am running Ubuntu 18.04.6 and Python 3.8.13.
Update:
The problem has "spread"; it's occurring in other directories now too, but not all directories. Also, when I change my code to try to deregister the environment before registering, as follows, I get the following new error:
Modified code:
import gym
env_dict = gym.envs.registration.registry.env_specs.copy()
for env in env_dict:
if 'simglucose' in env:
print(f"Removing {env}")
del gym.envs.registration.registry.env_specs[env]
from gym.envs.registration import register
register(
id='simglucose-adolescent2-v0',
entry_point='simglucose.envs:T1DSimEnv',
kwargs={'patient_name': 'adolescent#002'}
)
env = gym.make('simglucose-adolescent2-v0')
New error:
ModuleNotFoundError: No module named 'simglucose.envs'; 'simglucose' is not a package
Strangely, when I run this modified code in the directories where the 1st error above is not occurring (still using the same pip/conda environment), this new error does not occur either. I have checked for hidden files and found nothing other the the __pychache__ directory. I tried deleting that, but it changes nothing.
import simglucose
worked fine, but
import simglucose.envs #gym was calling pkg_resources which was calling this statement
failed (depending on the directory) because my Python file was named simglucose.py. Renaming the file fixed the problem.
Somehow, this was also causing not only the error in the update above, but the original Cannot re-register id
error. The error no longer occurred after renaming the Python file (no need to deregister the environment as I did in the update).
TLDR: If you are getting an error like this, make sure your Python file names do not conflict with any module/package names.
Update: I also found a case where both the deregistration step (credit to https://stackoverflow.com/a/61648353/3187892) and this fix were both required, so do try both if neither is working by itself.