I am building a Python project into a standalone executable using PyInstaller, and I’m encountering an issue when trying to run a Python script (fec.py) as a subprocess from the main script (main.py). Everything works fine when I run the program in my IDE, but when I compile it to an .exe, the subprocess call to fec.py fails and nothing happens
I have two Python files:
main.py: The main script that handles user input and calls fec.py via subprocess.
fec.py: A separate script that takes arguments and performs a task.
I’m using subprocess.Popen to run fec.py from main.py with arguments:
def run_download(self, args):
try:
# Handling paths when running as an executable
if getattr(sys, 'frozen', False):
bundle_dir = sys._MEIPASS # Temp directory when packaged
else:
bundle_dir = os.path.dirname(os.path.abspath(__file__)) # Normal script location
fec_script_path = os.path.join(bundle_dir, 'fec.py')
# Debugging: Check if file exists
if not os.path.exists(fec_script_path):
raise FileNotFoundError(f"fec.py not found at {fec_script_path}")
# Running the fec.py script
process = subprocess.Popen([sys.executable, fec_script_path] + args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
print(f"stdout: {stdout}")
print(f"stderr: {stderr}")
except Exception as e:
print(f"Error: {str(e)}")```
How I am building the exe:
I am using this PyInstaller command to bundle both main.py and fec.py:
pyinstaller --noconfirm --onefile --console --add-data "C:UsershamzaOneDriveDokumenterinvoicefec.py;."
3