Running Python script with QProcess after freeze

393 Views Asked by At

I am using PyQt5 and freezing the app using fbs. The app allows for users to run Python scripts. I then save the script in a Python file and run a QProcess to run that file.

The scripts that will be running are written by the user. I want to be able to run scripts that import specific libraries like NumPy without having the user install the libraries themselves.

How can I run the QProcess from the Python environment that I am freezing? So that the users don't have to install anything to run their scripts other than my app.

Right now, after freezing, the QProcess uses the default Python environment installed on the machine.

2

There are 2 best solutions below

7
On

You can import user script in your app using importlib.machinery.SourceFileLoader

user_script.py

import numpy as np
print("hello from user_script.py")
print(np.zeros(1))

app.py

from importlib.machinery import SourceFileLoader
def load_user_script(path):
    SourceFileLoader("user_module", path).load_module()

to run in separate thread use QThread

from importlib.machinery import SourceFileLoader
from PyQt5 import QtCore

class Worker(QtCore.QThread):
    def __init__(self, path, parent = None):
        super().__init__(parent)
        self._path = path
    def run(self):
        SourceFileLoader("user_module", self._path).load_module()

worker = Worker(path)
worker.start()
5
On

You can't because you always need:

  • The interpreter (that is, Python) and

  • The dependencies of the libraries (for example, numpy needs some dlls).

What they propose in the another answer is only viable if you have python installed that interprets the script.