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

[Proposal] Can the dependency box2d-py==2.3.8 be replaced with Box2D==2.3.10, which will simplify the installation? #1324

Open
1 task done
moretouch opened this issue Mar 1, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@moretouch
Copy link

moretouch commented Mar 1, 2025

Proposal

replace dependency box2d-py==2.3.8 to Box2D==2.3.10, will simplify the installation.
hoping that more professional and trustworthy community members can test its function and check its security.

Motivation

During the process of learning gym, I am always blocked by the dependency box2d-py every time I install it. The official has stopped maintaining this project, and there are very few built distributions in the official pypi repository. There are a large number of similar installation errors in issues and on the internet. A c++ compilation environment is required to install the box2d-py dependency normally, which is actually unnecessary.

The community provides the Box2D package (https://pypi.org/project/Box2D/). I downloaded the project code, changed the dependency configuration in pyproject.toml to Box2D ==2.3.10, and made no other changes. Installation was very easy, and there were no issues with initial use. This should reduce the difficulty for many beginners.

This may require more testing. I'm not very sure about the security of the Box2D package. I'm just making a suggestion, hoping that more professional and trustworthy community members can test its function and check its security.

Pitch

....
[project.optional-dependencies]
# Update dependencies in `all` if any are added or removed
atari = ["ale_py >=0.9"]
box2d = ["Box2D ==2.3.10", "pygame >=2.1.3", "swig ==4.*"]

....

all = [
    # All dependencies above except accept-rom-license
    # NOTE: No need to manually remove the duplicates, setuptools automatically does that.
    # atari
    "ale_py >=0.9",
    # box2d
    "Box2D ==2.3.10",
....

]
....

Alternatives

No response

Additional context

No response

Checklist

  • I have checked that there is no similar issue in the repo
@moretouch moretouch added the enhancement New feature or request label Mar 1, 2025
@Wasserwecken
Copy link

Wasserwecken commented Mar 5, 2025

THANK YOU SOOO MUCH! Thats the solution! Finally!

Im using gym on windows and none of the existing issues did work. (#1009, #1127, #1292, #1300, #1309)

So what are the exact steps for my applied hacky workaround, based on @moretouch ? (Im using Win11 with Python 3.12)

  1. Get the gymansium package location on your system with pip show gymnasium
  2. Find gymnasium-1.1.0.dist-info in the parent directory site-packages
  3. Edit file METADATA by replacing all box2d-py==2.3.8 with Box2D==2.3.10
  4. Run pip install gymnasium[box2d]
  5. Run the lunar lander example

Image

@moretouch
Copy link
Author

moretouch commented Mar 5, 2025

I have provided a script to facilitate the installation of this library. Copy and save it in an empty folder, then run it. @Wasserwecken

import hashlib
import os
import shutil
import subprocess
import tarfile

def main():
    # 1. Use pip to download the gymnasium library
    print("Downloading gymnasium-1.1.0.tar.gz...")
    subprocess.run(
        ["pip", "download", "gymnasium==1.1.0", "--no-binary", ":all:", "--no-deps", "-d", "."],
        check=True
    )

    # 2. Verify the SHA256 hash
    print("Checking file SHA256...")

    def compute_sha256(filename):
        sha256 = hashlib.sha256()
        with open(filename, "rb") as f:
            while chunk := f.read(4096):
                sha256.update(chunk)
        return sha256.hexdigest()

    # this hex from https://pypi.org/project/gymnasium/#gymnasium-1.1.0.tar.gz
    expected_hash = "dedb5c8c83047d3927ef8b841fb4ebadaeaa43ab954e2e3aca7eadcf4226c5f2" 
    actual_hash = compute_sha256("gymnasium-1.1.0.tar.gz")
    if actual_hash != expected_hash:
        raise ValueError(f"Hash verification failed! Expected: {expected_hash}, Actual: {actual_hash}")

    # 3. Extract to the gymnasium folder
    print("Extracting file...")
    with tarfile.open("gymnasium-1.1.0.tar.gz", "r:gz") as tar:
        # Create the target directory
        os.makedirs("gymnasium", exist_ok=True)

        # Process the archive members' paths
        members = []
        for member in tar.getmembers():
            if member.name.startswith("gymnasium-1.1.0/"):
                # Remove the top-level directory structure
                member.name = member.name[len("gymnasium-1.1.0/"):]
                members.append(member)

        # Extract files
        tar.extractall(path="gymnasium", members=members)

    # 4. Modify the pyproject.toml file
    print("Modifying dependencies...")
    toml_path = os.path.join("gymnasium", "pyproject.toml")
    with open(toml_path, "r") as f:
        content = f.read()

    # Use regular expressions to match different version declarations
    new_content = content.replace('box2d-py ==2.3.5', 'Box2D ==2.3.10')

    with open(toml_path, "w") as f:
        f.write(new_content)

    # 5. Repackage as a new tar.gz file
    print("Repacking file...")
    with tarfile.open("gymnasium-1.1.0.tar.gz", "w:gz") as tar:
        # Add the modified content, preserving the original directory structure
        tar.add("gymnasium", arcname="gymnasium-1.1.0")

    print("The new file has been saved as gymnasium-1.1.0.tar.gz")
    # Remove the directory
    shutil.rmtree("gymnasium")
    _type = input('Please input the content to install: [box2d / all] > ')
    if _type:
        subprocess.run(['pip', 'install', './gymnasium-1.1.0.tar.gz[' + _type.strip() + ']'])
    else:
        subprocess.run(['pip', 'install', './gymnasium-1.1.0.tar.gz[all]'])
    os.unlink("gymnasium-1.1.0.tar.gz")

if __name__ == "__main__":
    main()

@pseudo-rnd-thoughts
Copy link
Member

I would be interested in investigating this; however, it is not as simple as it appears.

box2d-py is an OpenAI project - https://github.com/openai/box2d-py (https://pypi.org/project/box2d-py/)
box2d is a actually https://github.com/pybox2d/pybox2d that the OpenAI project appeared to copy. Importantly, this is different from https://github.com/erincatto/box2d which I believe is the original base project that doesn't have python bindings.

To make the change, we would need to confirm that there is no impact for agents using box2d-py and box2d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants