1

I am trying to compile a python file into a .exe from within another script. The main issue I am having is with the command to build it as I need to input the file path which I have as a variable in my code. When I try and run it, it takes the name of the variable as the input into the command no matter what way I have the syntax. I may have explained this pretty badly but any help would be appreciated.

import string
import PyInstaller
import subprocess
import winapps
import PySimpleGUI as sg
import os.path
import ctypes

file_list_column = [
    [
        sg.Text("File Path"),
        sg.In(size=(25, 1), enable_events=True, key="-File Path-"),
        sg.Button(size=(10, 1), enable_events=True, key="-AddButton-", button_text="Add"),
    ],
    [
        sg.Listbox(
            values=[], enable_events=True, size=(40, 20), key="-File List-"
        )
    ],
    [
        sg.Button(size=(15, 1), enable_events=True, key="-create-", button_text="Create")
    ],
]

layout = [
    [
        sg.Column(file_list_column),
    ]
]

window = sg.Window("Startup Script", layout)
subInput = ""
subnum = 0
file_list = []

while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    elif event == "-AddButton-":
        new_filename = values["-File Path-"].strip()
        if new_filename not in file_list:
            file_list = sorted(file_list + [new_filename])
            print(file_list)
            values = file_list
            window["-File List-"].update(values)
    elif event == "-create-":
        print("create")
        for inputs in file_list:
            subInput = "subprocess.Popen(r'" + inputs + "')\n"
            print("added to file")
            filename = "subprocess" + str(subnum) + ".py"
            if not os.path.exists(filename):
                f = open(filename, "x")
                f.write("import subprocess\n")
                f.write(subInput)
                f.close()
                subnum = subnum + 1
            else:
                subnum = subnum + 1
        installPath = os.path.abspath(filename)
        os.system('cmd /k"PyInstaller" + installPath')

window.close()
# subprocess.Popen(r"C:\Users\templ\AppData\Local\Discord\app-1.0.9004\Discord.exe")
# subprocess.Popen(r"C:\Program Files (x86)\Steam\Steam.exe")
2
  • I read those middle two sentences a few times and still couldn't make it out... but looking at the code I think you mean here os.system('cmd /k"PyInstaller" + installPath') ... you want the value of installPath variable to be used in the string you pass to os.system? Commented Jul 8, 2022 at 12:35
  • Yea thats what im trying to do, I explained it really badly Commented Jul 8, 2022 at 13:12

1 Answer 1

2

I did not run the entire script you provided but based on the comment from Anentropic try replacing your line

os.system('cmd /k"PyInstaller" + installPath')

with either

os.system('cmd /k"PyInstaller" ' + installPath)

or

os.system(f'cmd /k"PyInstaller" {installPath}')

or (the least preferred) with

os.system('cmd /k"PyInstaller" {}'.format(installPath))

so that the value that you store into installPath is used

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.