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

Question:

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.

Asked By: Taiko

||

Answers:

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
Answered By: mknull

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}")
Answered By: Taiko

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.

Answered By: m_from_space
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.