PYTHON module "requests" not working with packaged electron app

Question:

In my electron app I have a python file that has two import statements shown below

import requests
import shutil

The app works fine without an errors in the IDE but after packaging the electron app with

npx electron-packager ./ --platform=darwin --icon=src/img/logo.icns

The app gives me this error
Uncaught Error: ModuleNotFoundError: No module named 'requests'

It only gives this error for the ‘requests’ module but not the ‘shutil’ module. And yes, ‘requests’ is installed on my computer. I also use the PythonShell module in the js file to run the python file like below

function version_checker(){
    form.classList.toggle('hide');
    circle.classList.toggle('show');
    let pyshell = new PythonShell(__dirname + '/version_checker.py');

    pyshell.on('message', function (message){
        console.log(message);
        if(message !== "same"){
            console.log("updating")
        }
        else{

            if(message !== "restart"){
                form.classList.toggle('hide');
                circle.classList.toggle('show');
            }
            else{}
        }
    })
    pyshell.end(function (err,code,signal) {
        if (err) throw err;
        console.log('The exit code was: ' + code);
        console.log('The exit signal was: ' + signal);
        console.log('finished');
    });
}

I also do not wish to compile the python file into an executable because the python script "talks" to the js file. I am also on a M1 Pro Macbook running macOS Ventrua if that helps

How do I package the app and include the ‘requests’ module OR How do I make the ‘requests’ module work with the packaged app

I did find a stackoverflow post that asked a similar question but the owner voluntary took down the question so I wasn’t able to view it

I looked at this post but it wasn’t helpful because I’m not using ‘anaconda’ or ‘child-process’

Asked By: Someone

||

Answers:

If the error is ModuleNotFoundError, then your python interpreter is struggling to find the requests module. This can happen for a number of reasons:

  1. Module does not exist: We can rule this out if your ide ran program successfully.

2: Module is not where interpreter expects to find it: Most likely.

Almost certainly the python interpreter used by your IDE is different to the one you ran after packaging the electron app.

The solution is to either:

a. make sure the location of requests is visible to the PYTHONPATH by setting that in the proper location (I don’t use MAC but I believe you have ~/.bash_profile file to set it in or

b. Explicitly add the request module location via a sys.path.append command in your code eg

import sys
import os

path = 'your path to request module'
sys.path.append(os.path.join(path, 'requests')

import requests

You should be able to find the path location by investigating your IDE and looking where you set the module to be included in your program

Answered By: Galo do Leste

PythonShell just calls the system’s Python interpreter, so use PIP to install the requests library

Or specify the interpreter as follows:

new PythonShell(__dirname + '/version_checker.py', {
  pythonPath: '/path/to/python'
})

Note: For production purposes, it is recommended to include an interpreter in electron and install dependencies

It seems you just want to make a HTTP request, why not try fetch in javascript?

Answered By: TES286