padi Foley
padi Foley

Reputation: 17

Building a python file from within another python script

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")

Upvotes: 1

Views: 63

Answers (1)

Michael
Michael

Reputation: 492

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

Upvotes: 2

Related Questions