Convert .py file to .exe file (I used text files and images to build the py file)
I would reccommend using auto-py-to-exe, a wrapper program that simplifies a lot of the tasks of using pyinstaller, a program that bundles python and the scripts into an executable.
pip install auto-py-to-exe
it can then be run with
auto-py-to-exe
It should open a new window where you can configure options (such as if you want to have a single executable file or if averything is in a folder. If your assets (images, etc.) are in a different folder, then you may have to add them to the bundle by configuring the options in the auto-py-to-exe window.
Convert .py to .exe with cmd
While Eyal's solution is correct, there is an easier option. Just run this:
py -m PyInstaller --onefile main.py
It could also be python -m PyInstaller --onefile main.py
depending on your system configuration.
If I convert a python file from py to exe, what about the module I created that the file imports?
It looks like Auto PY to EXE uses PyInstaller to do the real work here:
A .py to .exe converter using a simple graphical interface and PyInstaller in Python.
PyInstaller will automatically bundle any dependencies it finds:
What other modules and libraries does your script need in order to run? (These are sometimes called its "dependencies".)
To find out, PyInstaller finds all the
import
statements in your script. It finds the imported modules and looks in them forimport
statements, and so on recursively, until it has a complete list of modules your script may use.
So as long as you import
your dependencies somewhere reachable from your main script and your virtual environment is active, you should get the right behaviour automatically.
Note that you'll probably need to build two executables: one for your server and a separate one for your client.
How to turn python to exe with only needed parts of library?
You could create a virtual environment where you can install your required modules and then create the exe file in that environment itself. The python base library would be there anyway. Check out : How to include only needed modules in pyinstaller?
How to make a program convert a .PY file to .EXE without human intervention and without GUI
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")
Related Topics
Wait Until Page Is Loaded With Selenium Webdriver For Python
How to Call a Function Within a Class
How to Fix: "Unicodedecodeerror: 'Ascii' Codec Can't Decode Byte"
How to Properly Ignore Exceptions
How to Search For a String in Text Files
How to Send an Email With Gmail as Provider Using Python
Convert String to Variable Name in Python
How to Search and Replace Text in a File
What's the Difference Between Eval, Exec, and Compile
Changing Default Encoding of Python
Manually Raising (Throwing) an Exception in Python