Ren'Py : ModuleNotFoundError: No module named 'netrc'

942 Views Asked by At

Trying to plug openai's API to Ren'Py (to make characters answer to the player)

I installed the openai python module in Ren'Py using this pip command :

pip install --target /Users/...../GameName/game/python-packages netrc

And then inside the game I use this to import the module (as expalined in the Ren'Py documentation here : https://www.renpy.org/doc/html/python.html

init python:
    import openai

But I get this error :

File "python-packages/aiohttp/helpers.py", line 9, in ModuleNotFoundError: No module named 'netrc'

I guess it means that Ren'Py runs a custom python environment without netrc, but I don't know how to install netrc as a module in Ren'Py

Any help would be greatly appreciated, I'll gladly open source this GPT-3 powered Ren'Py project once it starts working.

3

There are 3 best solutions below

1
m_from_space On BEST ANSWER

Renpy 8.0.3 doesn't ship with the necessary packages of the Python standard library (in this case 'netrc' and '_multibytecodec'). I asked the main developer about it. He now included more of the standard library as part of the Renpy nightly build, which of course will make its way into the next release.

The current nightly build supports the required modules.

So importing the openai module as described earlier is now possible. Just make sure to delete the "certifi" module from python-packages, as it is already part of the Renpy environment.

2
mknull On

You just have to install the dependencies in the project's files. To do so, go to the project's directory and run

pip install --target game/python-packages netrc
0
Taiko On

There was really no way around, but actually it was super simple to just re-program the openai package using "requests" which is a package supported by renpy

https://github.com/Taiko3615/RenPyChatGPTExample

# Import required libraries
import requests
import json

# Define the completion function that takes messages and an API key as input
def completion(messages, api_key):
    # Set the API endpoint URL for ChatGPT completions
    url = "https://api.openai.com/v1/chat/completions"

    # Set the headers for the API request, including the Content-Type and Authorization with the API key
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    # Set the data for the API request, including the model and the input messages
    data = {
        "model": "gpt-3.5-turbo",
        "messages": messages
    }

    # Send the API request using the POST method, passing the headers and the data as JSON
    response = requests.post(url, headers=headers, data=json.dumps(data))

    # Check if the response status code is 200 (successful)
    if response.status_code == 200:
        # Extract the message from the response JSON and append it to the messages list
        completion = response.json()["choices"][0]["message"]
        messages.append(completion)
        return messages  # Return the updated messages list
    else:
        # If the status code is not 200, raise an exception with the error details
        raise Exception(f"Error: {response.status_code}, {response.text}")