Intel® Developer Cloud
Help connecting to or getting started on Intel® Developer Cloud
156 Discussions

import gymnasium error with Jupyter Notebook

HakanBa
Beginner
809 Views

After 

!pip install gymnasium stable_baselines3 Shimmy 

 

I am getting error with import. And this does not happen in Google Colab;

import gymnasium 

---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[13], line 1
----> 1 import gymnasium

ModuleNotFoundError: No module named 'gymnasium'

 

I have tried all the kernels and no luck so far.  Is there a solution for this ? 

Thank you in advance. -Hakan

 

 

 

 

 

Labels (1)
0 Kudos
10 Replies
Luwe_Intel
Moderator
760 Views

Hi HakanBa,

Thank you for reaching out to us.

 

Could you kindly launch a new notebook under base kernel by clicking the New Launcher button on the top left side of JupyterLab and try to run the codes again?

 base kernel.png

 

I have validated on my end, and I can execute the codes using base kernel without any issue at this moment:

install gymnasium.png

import gymnasium.png

 

Do let us know if it works for you.

 

 

Regards,

Luwe

 

0 Kudos
HakanBa
Beginner
710 Views


Hi Luwe,
Thank ou for your time.

I have tried that but I get following error;

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[2], line 1
----> 1 import gymnasium

File ~/.local/lib/python3.9/site-packages/gymnasium/__init__.py:4
1 """Root `__init__` of the gymnasium module setting the `__all__` of gymnasium modules."""
2 # isort: skip_file
----> 4 from gymnasium.core import (
5 Env,
6 Wrapper,
7 ObservationWrapper,
8 ActionWrapper,
9 RewardWrapper,
10 )
11 from gymnasium.spaces.space import Space
12 from gymnasium.envs.registration import (
13 make,
14 spec,
(...)
19 register_envs,
20 )

File ~/.local/lib/python3.9/site-packages/gymnasium/core.py:9
5 from typing import TYPE_CHECKING, Any, Generic, SupportsFloat, TypeVar
7 import numpy as np
----> 9 from gymnasium import logger, spaces
10 from gymnasium.utils import RecordConstructorArgs, seeding
13 if TYPE_CHECKING:

ImportError: cannot import name 'logger' from partially initialized module 'gymnasium' (most likely due to a circular import) (/home/ue8699a54d2b9b3ad864ce96dcfa33ae/.local/lib/python3.9/site-packages/gymnasium/__init__.py)
 

0 Kudos
Luwe_Intel
Moderator
651 Views

Hi HakanBa,

Could you kindly provide us your codes for further investigation of the error? For your information, the JupyterLab machines are intended for training purposes and workshops only. May I know which training are you running in Jupyter Notebook?

 

 

Regards,

Luwe


0 Kudos
HakanBa
Beginner
641 Views

I am creating a custom gymnasium enviroment.  gymnasium is more formal than OpenAI gym.  

And I need to be able to  import gymnasium but I cannot. And I can do this  with Google colab 

and Jupyter Lab.   

 

I think Intel provides jupyter notebook enviroment right ?    

 

If  import gymnasium works I will solve my problem. 

 

As I posted in my original question  I install the required packages;

 

!pip install gymnasium stable_baselines3 Shimmy 

 

My code: 

 

import gymnasium

def sorting_action_constraint_fn(action):
# Customize the constraint logic based on your requirements
i, j= action
return i != j


class GymnasiumSortingEnv(gymnasium.Env):
def __init__(self, sequence_length):
super(GymnasiumSortingEnv, self).__init__()

self.sequence_length = sequence_length
self.observation_space = gymnasium.spaces.Box(low=0, high=1, shape=(sequence_length,), dtype=np.float32)
self.action_space = gymnasium.spaces.MultiDiscrete([sequence_length, sequence_length])

self.current_sequence = np.zeros(sequence_length)
#self.reset()


def reset(self,seed=None, options=None):
self.current_sequence = np.random.rand(self.sequence_length)
#return self.current_sequence.copy()
return self.current_sequence.copy(), {}

def is_valid_action(self, action):
# Define your constraint function here
return sorting_action_constraint_fn(action)

def step(self, action):
# Validate the action
assert self.action_space.contains(action), f"Invalid action: {action}"

# Swap the elements at the specified indices
i, j = action

#if not sorting_action_constraint_fn(action):
# raise ValueError("Invalid action. Please choose a valid action.")

self.current_sequence[i], self.current_sequence[j] = self.current_sequence[j], self.current_sequence[i]

# Calculate reward
#reward = -np.sum(np.abs(np.diff(self.current_sequence))) # Negative sum of absolute differences
#reward = custom_distance_metric(self.target_sequence, self.current_sequence)

reward = 1 if np.all(np.diff(self.current_sequence) >= 0) else 0

# Check if the sequence is sorted
done = np.all(np.diff(self.current_sequence) >= 0)

# Return the new state, reward, whether the episode is done, and additional info

return self.current_sequence.copy(), reward, done, False, {}

def render(self):
print("Current Sequence:", self.current_sequence)

0 Kudos
Luwe_Intel
Moderator
603 Views

Hi HakanBa,

Thank you for the codes provided. I am able to execute them using base kernel without facing any error at this moment:

gymanasium code 1.png

gymnasium code 2.png

 

Kindly verify with us if the packages that you have installed are the same version as ours. I have attached my packages version below for your verification. You may validate it by executing the following command:

pip list

 

 

Regards,

Luwe

0 Kudos
HakanBa
Beginner
594 Views

Thank you Luwe. 

I  have provided the list of packages in the attachment.  

-Hakan

0 Kudos
HakanBa
Beginner
561 Views

Is it possible that  my  container is using different type of image than yours ?   

0 Kudos
HakanBa
Beginner
557 Views

I think I solved the problem. 

 

print(sys.path)


['/opt/intel/oneapi/ospray_studio/1.0/lib',
'/opt/intel/oneapi/advisor/2024.1/pythonapi',
'/opt/intel/oneapi/intelpython/lib/python39.zip',
'/opt/intel/oneapi/intelpython/lib/python3.9',
'/opt/intel/oneapi/intelpython/lib/python3.9/lib-dynload',
'',
'/home/<myuserid>/.local/lib/python3.9/site-packages',
'/opt/intel/oneapi/intelpython/lib/python3.9/site-packages']

 


I  have cleaned the folders under /home/<myuserid>/.local/lib/python3.9/site-packages  

And  installed the packages again. 

 

 

 

 

 

0 Kudos
Luwe_Intel
Moderator
424 Views

Hi HakanBa,

Could you execute the following codes to redownload and reinstall the package:

!pip install --force-reinstall --no-cache-dir gymnasium stable_baselines3 Shimmy

 

Do let us know if it resolves the error.

 

 

Regards,

Luwe

 

0 Kudos
Luwe_Intel
Moderator
408 Views

Hi HakanBa,

This thread will no longer be monitored since this issue has been resolved. If you need any additional information from Intel, please submit a new question.

 

 

Regards,

Luwe


0 Kudos
Reply