0

Could I run a function from another python file inside subprocess?

I use pyinstaller to convert the tkinter to an executable file. As much as possible I would like to deploy/run the .exe file to another computer without installing python.

#gui.py
from tkinter import *
import os
import subprocess

root = Tk()
root.geometry("300x150")

def backendStart():
  subprocess.Popen(["test.py"], shell=True)

label = Label(root, text="Connection String", fg="grey", font=("Helvetica", 15, "bold"))
label.pack(pady=(0,3))


root.after_idle(backendStart)
root.mainloop()

Here is my sample app.py

from flask import Flask, jsonify
from flask_restful import Api, Resource
from flask_socketio import SocketIO, emit
from flask_cors import CORS
import random
from connection import cursor

app = Flask(__name__)
app.config["DEBUG"] = True
api = Api(app)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")

@socketio.on("connect")
def ClientConnection():
  print("Client is Connected")

@socketio.on("realtime", namespace="/sample-socket")
def RealTimeData():
  while True:
    num = random.randint(1, 100)
    emit("data", {"random": num})
    socketio.sleep(1)

@socketio.on("disconnect")
def ClientDisconnected():
  print("client has disconnected")

class HomePage(Resource):
  def get(self):
    return jsonify(msg="hello world")

api.add_resource(HomePage, "/")

if __name__ == '__main__':
  socketio.run(app, host="192.168.0.109", port=5000)

Currently I made a .spec file for configurating the names, logo, and files/libs included. The .exe file work as long as I pasted the app.py inside the build folder along with the connection.py for the database.

But with this set up I do need to install python along with the libraries I used for the app.py and connection.py

gfdsweds
  • 345
  • 3
  • 11
Nellartsa
  • 29
  • 1
  • 7
  • if you converted a python file to an executable and it has all the dependencies where they should be, it should work on other computers without needing to install python on them (if they can handle the `.exe`) (the whole point of converting to an executable is the program can be easily distributed without users having to install additional software) – Matiiss Aug 02 '21 at 05:46
  • tkinter isn't designed to run via a server. Are you expecting the window to appear in a browser? It will only appear on a display connected to the server. – Bryan Oakley Aug 02 '21 at 05:55
  • @Matiiss that's what I'm trying to do but the subprocess use `app.py` file instead of the function that calls the `socketio.run()` (Already tried making a function and call it on `gui.py`). So currently it only works if I paste the `app.py` inside the build folder and python is installed along with the libraries – Nellartsa Aug 03 '21 at 03:52
  • @BryanOakley I'll be using `tkinter` just to run and stop the `flask api`. So I would like it to work like `XAMPP` cause the projects that I'm doing is only deployed locally. – Nellartsa Aug 03 '21 at 03:53
  • ok, so I just answered another question that may answer your question too (your question is what actually made me answer that other question): https://stackoverflow.com/a/68672448/14531062 – Matiiss Aug 05 '21 at 19:24

1 Answers1

2
Could I run a function from another 
python file inside subprocess?

you can use multiprocessing.Process()

Edit:
For an expert's answer, see here
Alternate solution:
Lets say you want to run app.exe (which is app.py), you can pack all of them into one folder (--onedir) and put those exe's and pyd's subfolders.... together, like

gui
----flask (and other folders)
----*.pyd
----app.exe
----gui.exe
----python39.dll

you will need a custom spec file to do that.
see https://pyinstaller.readthedocs.io/en/stable/spec-files.html#multipackage-bundles

gfdsweds
  • 345
  • 3
  • 11
  • If I did that how would I call the `app.exe` if I run the `gui.exe`. And does separating them means they would individually open a console? – Nellartsa Aug 03 '21 at 03:58
  • @Nellartsa in that case you could actually simply run the other `.exe` using python, you could then when compiling the flask main file to exe make them not open the cmd window, but I think if you then were to use `subprocess.Popen` and run the exe you could still stream data from there and then put that data wherever you need – Matiiss Aug 03 '21 at 04:44
  • Just clarifying are you saying that I should make `app.py` as an `.exe` file without the console. Then reconstruct `gui.py` so I could call the `app.exe` when I run the `gui.exe`. – Nellartsa Aug 03 '21 at 05:26
  • @Nellartsa Yes, thats what i want. – gfdsweds Aug 03 '21 at 06:02
  • 1
    @Nellartsa I find this discussion to be useful: https://github.com/pyinstaller/pyinstaller/discussions/6046 – gfdsweds Aug 03 '21 at 06:46
  • @gfdsweds I'll try to reconstruct some parts of my project and hopefully I could make it work how I wanted it to. Thanks you – Nellartsa Aug 03 '21 at 07:17