Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] Installing gymnasium[box2d] fails #245

Closed
1 task done
vcharraut opened this issue Jan 7, 2023 · 13 comments
Closed
1 task done

[Bug Report] Installing gymnasium[box2d] fails #245

vcharraut opened this issue Jan 7, 2023 · 13 comments
Labels
bug Something isn't working

Comments

@vcharraut
Copy link
Contributor

vcharraut commented Jan 7, 2023

Describe the bug

When running poetry add gymnasium[box2d] or pip install gymnasium[box2d], the building of the wheel for box2d-py produces an error :

ModuleNotFoundError: No module named 'swig'

Code example

> poetry add gymnasium[box2d]

Using version ^0.27.0 for gymnasium

Updating dependencies
Resolving dependencies... (0.1s)

Package operations: 1 install, 0 updates, 0 removals

  • Installing box2d-py (2.3.5): Failed

  CalledProcessError

  Command '['/var/home/valentin/Work/test/.venv/bin/python', '-m', 'pip', 'install', '--use-pep517', '--disable-pip-version-check', '--isolated', '--no-input', '--prefix', '/var/home/valentin/Work/test/.venv', '--no-deps', '/var/home/valentin/.cache/pypoetry/artifacts/58/b8/0a/6eb5d1b9c876fbc069c4af2c3c65cc6d4d002d4fb42533ed8a4ae7c943/box2d-py-2.3.5.tar.gz']' returned non-zero exit status 1.

  at /usr/lib64/python3.11/subprocess.py:571 in run
       567│             # We don't call process.wait() as .__exit__ does that for us.
       568│             raise
       569│         retcode = process.poll()
       570│         if check and retcode:
    →  571│             raise CalledProcessError(retcode, process.args,
       572│                                      output=stdout, stderr=stderr)
       573│     return CompletedProcess(process.args, retcode, stdout, stderr)
       574│ 
       575│ 

The following error occurred when trying to handle this error:


  EnvCommandError

  Command ['/var/home/valentin/Work/test/.venv/bin/python', '-m', 'pip', 'install', '--use-pep517', '--disable-pip-version-check', '--isolated', '--no-input', '--prefix', '/var/home/valentin/Work/test/.venv', '--no-deps', '/var/home/valentin/.cache/pypoetry/artifacts/58/b8/0a/6eb5d1b9c876fbc069c4af2c3c65cc6d4d002d4fb42533ed8a4ae7c943/box2d-py-2.3.5.tar.gz'] errored with the following return code 1, and output: 
  Processing /var/home/valentin/.cache/pypoetry/artifacts/58/b8/0a/6eb5d1b9c876fbc069c4af2c3c65cc6d4d002d4fb42533ed8a4ae7c943/box2d-py-2.3.5.tar.gz
    Installing build dependencies: started
    Installing build dependencies: finished with status 'done'
    Getting requirements to build wheel: started
    Getting requirements to build wheel: finished with status 'done'
    Preparing metadata (pyproject.toml): started
    Preparing metadata (pyproject.toml): finished with status 'done'
  Building wheels for collected packages: box2d-py
    Building wheel for box2d-py (pyproject.toml): started
    Building wheel for box2d-py (pyproject.toml): finished with status 'error'
    error: subprocess-exited-with-error
    
    × Building wheel for box2d-py (pyproject.toml) did not run successfully.
    │ exit code: 1
    ╰─> [20 lines of output]
        Using setuptools (version 65.6.3).
        running bdist_wheel
        running build
        running build_py
        creating build
        creating build/lib.linux-x86_64-cpython-311
        creating build/lib.linux-x86_64-cpython-311/Box2D
        copying library/Box2D/Box2D.py -> build/lib.linux-x86_64-cpython-311/Box2D
        copying library/Box2D/__init__.py -> build/lib.linux-x86_64-cpython-311/Box2D
        creating build/lib.linux-x86_64-cpython-311/Box2D/b2
        copying library/Box2D/b2/__init__.py -> build/lib.linux-x86_64-cpython-311/Box2D/b2
        running build_ext
        building 'Box2D._Box2D' extension
        swigging Box2D/Box2D.i to Box2D/Box2D_wrap.cpp
        swig -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library/Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D/Box2D_wrap.cpp Box2D/Box2D.i
        Traceback (most recent call last):
          File "/var/home/valentin/Work/test/.venv/bin/swig", line 5, in <module>
            from swig import swig
        ModuleNotFoundError: No module named 'swig'
        error: command '/var/home/valentin/Work/test/.venv/bin/swig' failed with exit code 1
        [end of output]
    
    note: This error originates from a subprocess, and is likely not a problem with pip.
    ERROR: Failed building wheel for box2d-py
  Failed to build box2d-py
  ERROR: Could not build wheels for box2d-py, which is required to install pyproject.toml-based projects
  

  at ~/.local/share/pypoetry/venv/lib64/python3.11/site-packages/poetry/utils/env.py:1540 in _run
      1536│                 output = subprocess.check_output(
      1537│                     command, stderr=subprocess.STDOUT, env=env, **kwargs
      1538│                 )
      1539│         except CalledProcessError as e:
    → 1540│             raise EnvCommandError(e, input=input_)
      1541│ 
      1542│         return decode(output)
      1543│ 
      1544│     def execute(self, bin: str, *args: str, **kwargs: Any) -> int:

The following error occurred when trying to handle this error:


  PoetryException

  Failed to install /var/home/valentin/.cache/pypoetry/artifacts/58/b8/0a/6eb5d1b9c876fbc069c4af2c3c65cc6d4d002d4fb42533ed8a4ae7c943/box2d-py-2.3.5.tar.gz

  at ~/.local/share/pypoetry/venv/lib64/python3.11/site-packages/poetry/utils/pip.py:58 in pip_install
       54│ 
       55│     try:
       56│         return environment.run_pip(*args)
       57│     except EnvCommandError as e:
    →  58│         raise PoetryException(f"Failed to install {path.as_posix()}") from e
       59│

System info

The bug is produced with poetry add or/and pip install.
Using Gymnasium 0.27 and Python 3.11

Additional context

In the pyproject.toml of Gymnasium, the box2d dependency is written as follow:

line 42 box2d = ["box2d-py ==2.3.5", "pygame ==2.1.3.dev8", "swig ==4.*"]

Since swig is required to build box2d-py, I think moving swig before box2d-py in the dependencies could solve the issue.

I would also need to be modified at line 63

all = [
    # All dependencies above except accept-rom-license
    # NOTE: No need to manually remove the duplicates, setuptools automatically does that.
    # atari
    "shimmy[atari] >=0.1.0,<1.0",
    # box2d
    "box2d-py ==2.3.5",
    "pygame ==2.1.3.dev8",
    "swig ==4.*",

The solution I use now is to specify the dependency of box2d-py before gymnasium in the pyproject.toml of my project, as follow :

[tool.poetry.dependencies]
python = "^3.10"
box2d-py = "2.3.5"
numpy = "^1.24.0"
torch = "1.13.1"
tensorboard = "^2.11.0"
gymnasium = {extras = ["all"], version = "^0.27.0"}

Checklist

  • I have checked that there is no similar issue in the repo
@vcharraut vcharraut added the bug Something isn't working label Jan 7, 2023
@kir0ul
Copy link
Contributor

kir0ul commented Jan 7, 2023

It seems to be specific to Poetry, as I can reproduce this error when installing with Poetry, but it works for me when installing with Pip.
According to python-poetry/poetry#6839 and https://github.com/nightlark/swig-pypi, adding the following to the pyproject.toml should solve the issue, but I can't make it work:

[build-system]
- requires = ["setuptools >= 61.0.0"]
+ requires = ["setuptools >= 61.0.0", "swig"]
build-backend = "setuptools.build_meta"

Other related issue: jonasschneider/box2d-py#1

@vcharraut
Copy link
Contributor Author

It seems to be specific to Poetry, as I can reproduce this error when installing with Poetry, but it works for me when installing with Pip. According to python-poetry/poetry#6839 and https://github.com/nightlark/swig-pypi, adding the following to the pyproject.toml should solve the issue, but I can't make it work:

[build-system]
- requires = ["setuptools >= 61.0.0"]
+ requires = ["setuptools >= 61.0.0", "swig"]
build-backend = "setuptools.build_meta"

Other related issue: jonasschneider/box2d-py#1

Thank you for your research. You're right, I tried again to make it work only with pip outside of poetry and it worked.

I tried the solution you provide and I can't make it work too.

Since the problem is specific to Poetry, shall I close the issue here ?

@thavens
Copy link

thavens commented Feb 7, 2023

When running with poetry it doesn't seem to have the sys.path set right.
If you wanna make it work right with poetry, adding .venv/lib/python3.11/site-packages/ to the path made it work for me.
I added it right before from swig import swig in the .venv/bin/swig file.

@boclifton-MSFT
Copy link

boclifton-MSFT commented Feb 1, 2024

Hi folks, I'm seeing this error consistently for the past few days trying to get gymnasium to work. I see this same error, ModuleNotFoundError: No module named 'swig' when doing pip install gymnasium[box2d] or pip install box2d-py. I'm not using Poetry, nor any environment manager other than a basic Python venv.

Gymnasium itself seems to be installed correctly, as the initial example from the documentation works fine: gym.make('CartPole-v1', render_mode="human")

(also, worth noting, I'm relatively new to Python but well-versed in programming in general)

(.venv) PS C:\Users\me\Source\Repos\gymnasium-exploration\from-scratch> pip install gymnasium[box2d]
Requirement already satisfied: gymnasium[box2d] in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (0.29.1)
Requirement already satisfied: numpy>=1.21.0 in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (1.26.3)
Requirement already satisfied: cloudpickle>=1.2.0 in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (3.0.0)
Requirement already satisfied: typing-extensions>=4.3.0 in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (4.9.0)
Requirement already satisfied: farama-notifications>=0.0.1 in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (0.0.4)
Collecting box2d-py==2.3.5 (from gymnasium[box2d])
  Using cached box2d-py-2.3.5.tar.gz (374 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: pygame>=2.1.3 in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (2.5.2)
Requirement already satisfied: swig==4.* in c:\users\me\source\repos\gymnasium-exploration\from-scratch\.venv\lib\site-packages (from gymnasium[box2d]) (4.2.0)
Building wheels for collected packages: box2d-py
  Building wheel for box2d-py (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for box2d-py (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      Using setuptools (version 69.0.3).
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-310
      creating build\lib.win-amd64-cpython-310\Box2D
      copying library\Box2D\Box2D.py -> build\lib.win-amd64-cpython-310\Box2D
      copying library\Box2D\__init__.py -> build\lib.win-amd64-cpython-310\Box2D
      creating build\lib.win-amd64-cpython-310\Box2D\b2
      copying library\Box2D\b2\__init__.py -> build\lib.win-amd64-cpython-310\Box2D\b2
      running build_ext
      building 'Box2D._Box2D' extension
      swigging Box2D\Box2D.i to Box2D\Box2D_wrap.cpp
      swig.exe -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library\Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D\Box2D_wrap.cpp Box2D\Box2D.i
      Traceback (most recent call last):
        File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
          return _run_code(code, main_globals, None,
        File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
          exec(code, run_globals)
        File "C:\Users\me\Source\Repos\gymnasium-exploration\from-scratch\.venv\Scripts\swig.exe\__main__.py", line 4, in <module>  
      ModuleNotFoundError: No module named 'swig'
      error: command 'C:\\Users\\me\\Source\\Repos\\gymnasium-exploration\\from-scratch\\.venv\\Scripts\\swig.exe' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for box2d-py
Failed to build box2d-py
ERROR: Could not build wheels for box2d-py, which is required to install pyproject.toml-based projects

@pseudo-rnd-thoughts
Copy link
Member

Either install swig separately or pip install swig

@TIHan
Copy link

TIHan commented Mar 21, 2024

@boclifton-MSFT try python 3.11. It didn't work on 3.10 for me.

@Wasserwecken
Copy link

Does not work on 3.11 and 3.10 either. I got for python 3.10 the exact same log as @boclifton-MSFT
I do also get the same log by installing box2d speratly

@jim-rothrock
Copy link

jim-rothrock commented Nov 2, 2024

I am having a similar problem installing gymnasium[box2d]. I am using:

Ubuntu 24.04.1 LTS
Python 3.12.3
Gymnasium 1.0.0

I do the following:

Create the Python virtual environment:

sudo apt install python3-full
cd
mkdir python_venv
python3 -m venv /home/jimr/python_venv

In .bashrc, I add;
export PATH=$HOME/python_venv/bin:$PATH

Install swig:

cd
pip install swig

I also install the swig Debian package, in case it is needed.

Install Gymnasium with box2d:
pip install gymnasium[box2d]

Output:

Requirement already satisfied: gymnasium[box2d] in ./python_venv/lib/python3.12/site-packages (1.0.0)
Requirement already satisfied: numpy>=1.21.0 in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (2.1.2)
Requirement already satisfied: cloudpickle>=1.2.0 in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (3.1.0)
Requirement already satisfied: typing-extensions>=4.3.0 in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (4.12.2)
Requirement already satisfied: farama-notifications>=0.0.1 in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (0.0.4)
Collecting box2d-py==2.3.5 (from gymnasium[box2d])
  Using cached box2d-py-2.3.5.tar.gz (374 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: pygame>=2.1.3 in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (2.6.1)
Requirement already satisfied: swig==4.* in ./python_venv/lib/python3.12/site-packages (from gymnasium[box2d]) (4.2.1.post0)
Building wheels for collected packages: box2d-py
  Building wheel for box2d-py (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for box2d-py (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [20 lines of output]
      /tmp/pip-build-env-tfy7ysj3/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py:261: UserWarning: Unknown distribution option: 'test_suite'
        warnings.warn(msg)
      Using setuptools (version 75.3.0).
      running bdist_wheel
      running build
      running build_py
      creating build/lib.linux-x86_64-cpython-312/Box2D
      copying library/Box2D/__init__.py -> build/lib.linux-x86_64-cpython-312/Box2D
      copying library/Box2D/Box2D.py -> build/lib.linux-x86_64-cpython-312/Box2D
      creating build/lib.linux-x86_64-cpython-312/Box2D/b2
      copying library/Box2D/b2/__init__.py -> build/lib.linux-x86_64-cpython-312/Box2D/b2
      running build_ext
      building 'Box2D._Box2D' extension
      swigging Box2D/Box2D.i to Box2D/Box2D_wrap.cpp
      swig -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library/Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D/Box2D_wrap.cpp Box2D/Box2D.i
      Traceback (most recent call last):
        File "/home/jimr/python_venv/bin/swig", line 5, in <module>
          from swig import swig
      ModuleNotFoundError: No module named 'swig'
      error: command '/home/jimr/python_venv/bin/swig' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for box2d-py
Failed to build box2d-py
ERROR: Could not build wheels for box2d-py, which is required to install pyproject.toml-based projects

I do not encounter this problem when running with the following configuration:

Ubuntu 22.04.5 LTS
Python 3.10.12
Gymnasium 0.29.1

A possibly important difference between this configuration and the problem one is that I am not using a virtual environment in the configuration that works. Pip installs all of the packages in ~/.local/lib/python3.10/site-packages. I tried to do the same thing with Python 3.12.3, but "pip install gymnasium[box2d]" gave me an "error: externally-managed-environment" if I didn't use a virtual environment.

@jim-rothrock
Copy link

@pseudo-rnd-thoughts , regarding my comment above, should I post that as a new issue? I cannot reopen this one.

@pseudo-rnd-thoughts
Copy link
Member

@jim-rothrock This seems to be an issue with Swig rather than Gymnasium.
The error is ModuleNotFoundError: No module named 'swig' related to the Python swig project. If you uninstall it and install the swig project as a executable what happens?

@jim-rothrock
Copy link

I assume that "Python swig project" is the swig installed via pip, and "swig project as an executable" is the swig installed by the swig Debian package.

When the Debian swig package is installed and the pip swig package is not installed, pip install gymnasium[box2d] produces this:

Building wheels for collected packages: box2d-py
  Building wheel for box2d-py (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for box2d-py (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [38 lines of output]
      /tmp/pip-build-env-41qr1slk/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py:261: UserWarning: Unknown distribution option: 'test_suite'
        warnings.warn(msg)
      Using setuptools (version 75.3.0).
      running bdist_wheel
      running build
      running build_py
      creating build/lib.linux-x86_64-cpython-312/Box2D
      copying library/Box2D/__init__.py -> build/lib.linux-x86_64-cpython-312/Box2D
      copying library/Box2D/Box2D.py -> build/lib.linux-x86_64-cpython-312/Box2D
      creating build/lib.linux-x86_64-cpython-312/Box2D/b2
      copying library/Box2D/b2/__init__.py -> build/lib.linux-x86_64-cpython-312/Box2D/b2
      running build_ext
      building 'Box2D._Box2D' extension
      swigging Box2D/Box2D.i to Box2D/Box2D_wrap.cpp
      swig -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library/Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D/Box2D_wrap.cpp Box2D/Box2D.i
      Box2D/Common/b2Math.h:67: Warning 302: Redefinition of identifier 'b2Vec2' by %extend ignored,
      Box2D/Box2D_math.i:47: Warning 302: %extend definition of 'b2Vec2'.
      Box2D/Common/b2Math.h:158: Warning 302: Redefinition of identifier 'b2Vec3' by %extend ignored,
      Box2D/Box2D_math.i:168: Warning 302: %extend definition of 'b2Vec3'.
      Box2D/Common/b2Math.h:197: Warning 302: Redefinition of identifier 'b2Mat22' by %extend ignored,
      Box2D/Box2D_math.i:301: Warning 302: %extend definition of 'b2Mat22'.
      Box2D/Common/b2Math.h:271: Warning 302: Redefinition of identifier 'b2Mat33' by %extend ignored,
      Box2D/Box2D_math.i:372: Warning 302: %extend definition of 'b2Mat33'.
      Box2D/Collision/b2DynamicTree.h:44: Warning 312: Nested union not currently supported (ignored).
      Box2D/Common/b2Settings.h:144: Warning 506: Can't wrap varargs with keyword arguments enabled
      Box2D/Common/b2Math.h:91: Warning 509: Overloaded method b2Vec2::operator ()(int32) effectively ignored,
      Box2D/Common/b2Math.h:85: Warning 509: as it is shadowed by b2Vec2::operator ()(int32) const.
      creating build/temp.linux-x86_64-cpython-312/Box2D
      creating build/temp.linux-x86_64-cpython-312/Box2D/Collision/Shapes
      creating build/temp.linux-x86_64-cpython-312/Box2D/Common
      creating build/temp.linux-x86_64-cpython-312/Box2D/Dynamics/Contacts
      creating build/temp.linux-x86_64-cpython-312/Box2D/Dynamics/Joints
      x86_64-linux-gnu-g++ -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -I/home/jimr/python_venv/include -I/usr/include/python3.12 -c Box2D/Box2D_wrap.cpp -o build/temp.linux-x86_64-cpython-312/Box2D/Box2D_wrap.o -I. -Wno-unused
      Box2D/Box2D_wrap.cpp:176:11: fatal error: Python.h: No such file or directory
        176 | # include <Python.h>
            |           ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-g++' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for box2d-py
Failed to build box2d-py
ERROR: Could not build wheels for box2d-py, which is required to install pyproject.toml-based projects

If pip swig and Debian swig are installed simultaneously and I change PATH so that /usr/bin/swig is found before /home/jimr/python_venv/bin/swig, I get the "Python.h: no such file or directory" error, as above.

@pseudo-rnd-thoughts
Copy link
Member

Reading the error you need to install python-dev, https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory

@jim-rothrock
Copy link

Here is the process I used to run "pip install gymnasium[box2d]" successfully on Ubuntu 24.04.1 LTS with Gymnasium 1.0.0.

In .bashrc, I added the following lines. Replace "python_venv" with your virtual environment directory.

# Set PATH so that /usr/bin/swig is found before $HOME/python_venv/bin/swig.
# This is needed in order for "pip install gymnasium[box2d]" to work.
export PATH=$PATH:$HOME/python_venv/bin

I then executed the following commands. python3-dev was installed in order to get Python.h.

sudo apt install swig
sudo apt install python3-dev
pip install gymnasium[box2d]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants