microsoft / TextWorld

​TextWorld is a sandbox learning environment for the training and evaluation of reinforcement learning (RL) agents on text-based games.
Other
1.22k stars 187 forks source link

Unable to import textworld in colab #320

Open rfy1004 opened 1 year ago

rfy1004 commented 1 year ago

Hi, I'm running the notebook 'Building a simple agent' in colab and have encountered the following ImportError when I tried to import textworld.

/usr/local/lib/python3.10/dist-packages/tatsu/grammars.py in 4 import os 5 import functools ----> 6 from collections import defaultdict, Mapping 7 from copy import copy 8 from itertools import takewhile

ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/init.py)

I tried replacing 'from collections import Mapping' with 'from collections.ac import Mapping' and got this error instead:

/usr/local/lib/python3.10/dist-packages/textworld/envs/wrappers/tw_inform7.py in 91 92 ---> 93 class TWInform7(textworld.core.Wrapper): 94 """ 95 Wrapper to play Inform7 games generated by TextWorld.

AttributeError: partially initialized module 'textworld' has no attribute 'core' (most likely due to a circular import)

The installation of textworld was sucessfull.

rfy1004 commented 1 year ago

I just realized that it might be because colab updated the main kernel to python 3.10. However I'm still having the same issue in virtual environment using python 3.9.

MarcCote commented 1 year ago

Hi @rfy1004. Did you change the runtime after installing 3.9? You can change it in the Runtime dropdown menu. Eg. for py38 image

I followed this https://stackoverflow.com/a/71511943

Also, note that data.zip can now be downloaded from the latest release https://github.com/microsoft/TextWorld/releases/download/1.5.4/data.zip

rfy1004 commented 1 year ago

Thanks for the solutions for installing Python 3.8. I followed this https://stackoverflow.com/a/71511943 and successfully changed the runtime type to Python 3.8. However when I run import textworld it gives this error:


ImportError Traceback (most recent call last) Cell In[10], line 1 ----> 1 import textworld.gym

File /usr/local/lib/python3.8/site-packages/textworld/init.py:11 9 from textworld.core import EnvInfos, EnvInfoMissingError 10 from textworld.core import Environment, GameState, Agent ---> 11 from textworld.generator import Game, GameMaker, GameOptions 13 from textworld.generator import GenerationWarning 15 from textworld.helpers import make, play, start

File /usr/local/lib/python3.8/site-packages/textworld/generator/init.py:30 28 from textworld.generator.data import KnowledgeBase 29 from textworld.generator.text_grammar import Grammar ---> 30 from textworld.generator.maker import GameMaker 31 from textworld.generator.logger import GameLogger 34 class GenerationWarning(UserWarning):

File /usr/local/lib/python3.8/site-packages/textworld/generator/maker.py:27 25 from textworld.generator.graph_networks import DIRECTIONS 26 from textworld.render import visualize ---> 27 from textworld.envs.wrappers import Recorder 30 def get_failing_constraints(state, kb: Optional[KnowledgeBase] = None): 31 kb = kb or KnowledgeBase.default() File /usr/local/lib/python3.8/site-packages/textworld/envs/init.py:4 1 # Copyright (c) Microsoft Corporation. All rights reserved. 2 # Licensed under the MIT license. ----> 4 from textworld.envs.glulx.git_glulx import GitGlulxEnv 5 from textworld.envs.zmachine.jericho import JerichoEnv 6 from textworld.envs.tw import TextWorldEnv

File /usr/local/lib/python3.8/site-packages/textworld/envs/glulx/git_glulx.py:13 9 from pkg_resources import Requirement, resource_filename 11 from typing import Union ---> 13 from glk import ffi, lib 14 from io import StringIO 16 import textworld

ImportError: libffi.so.6: cannot open shared object file: No such file or directory

I tried to manually install glk but got this:

ERROR: No matching distribution found for glk

rfy1004 commented 1 year ago

I figured out that the above error occurred because colab uses libffi.so.8 instead of libffi.so.6. So I tried run this command ! find /usr/lib -name "libffi.so*" linking libffi to version 6. However when I tried to run import textworld again it gave the same attribute error as before:

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[21], line 1 ----> 1 import textworld.gym

File /usr/local/lib/python3.8/site-packages/textworld/init.py:11 9 from textworld.core import EnvInfos, EnvInfoMissingError 10 from textworld.core import Environment, GameState, Agent ---> 11 from textworld.generator import Game, GameMaker, GameOptions 13 from textworld.generator import GenerationWarning 15 from textworld.helpers import make, play, start

File /usr/local/lib/python3.8/site-packages/textworld/generator/init.py:30 28 from textworld.generator.data import KnowledgeBase 29 from textworld.generator.text_grammar import Grammar ---> 30 from textworld.generator.maker import GameMaker 31 from textworld.generator.logger import GameLogger 34 class GenerationWarning(UserWarning):

File /usr/local/lib/python3.8/site-packages/textworld/generator/maker.py:27 25 from textworld.generator.graph_networks import DIRECTIONS 26 from textworld.render import visualize ---> 27 from textworld.envs.wrappers import Recorder 30 def get_failing_constraints(state, kb: Optional[KnowledgeBase] = None): 31 kb = kb or KnowledgeBase.default()

File /usr/local/lib/python3.8/site-packages/textworld/envs/init.py:7 5 from textworld.envs.zmachine.jericho import JerichoEnv 6 from textworld.envs.tw import TextWorldEnv ----> 7 from textworld.envs.wrappers.tw_inform7 import TWInform7

File /usr/local/lib/python3.8/site-packages/textworld/envs/wrappers/init.py:9 7 from textworld.envs.wrappers.filter import Filter 8 from textworld.envs.wrappers.limit import Limit ----> 9 from textworld.envs.wrappers.tw_inform7 import TWInform7 10 from textworld.envs.wrappers.generic import GenericEnvironment

File /usr/local/lib/python3.8/site-packages/textworld/envs/wrappers/tw_inform7.py:93 88 matches = [m for m in matches if "(" not in m and ")" not in m] 90 return matches, text ---> 93 class TWInform7(textworld.core.Wrapper): 94 """ 95 Wrapper to play Inform7 games generated by TextWorld. 96 """ 98 def _wrap(self, env):

AttributeError: partially initialized module 'textworld' has no attribute 'core' (most likely due to a circular import)`