The_Fishy
The_Fishy

Reputation: 143

How to make a program convert a .PY file to .EXE without human intervention and without GUI

After running my python program a myFile.py file has been created that contains some code. I want my program to not only create myFile.py but also convert it to myFile.exe.

auto-py-to-exe or any alternative that have the same problem isn't a solution because I need to manually select the file that I want to convert in a GUI to convert it, I need my program to convert it.

Upvotes: 0

Views: 626

Answers (1)

The_Fishy
The_Fishy

Reputation: 143

I'll be using the cx_Freeze library to convert .py to .exe. To install the package enter the command python -m pip install pypiwin32 in the terminal.

import win32com.client

tempFile = open(os.path.abspath("temp\\temp.py"), 'w')
your_File = 'yourFile.py'
print("import sys\nfrom cx_Freeze import Executable, setup\n\n\nbase = None\nif sys.platform == \"win32\":\n    base = \"Win32GUI\"\nexecutables = [Executable(r\"" + yourFile + "\", base = base)]\n\nsetup(\n    name=\"exe_File\",\n    version=\"0.1\",\n    description=\"Sample cx_Freeze script\",\n    executables=executables,\n)", file = tempFile)
tempFile.close()

os.system("python \"temp\\temp.py\" build")

Upvotes: 1

Related Questions