How to Convert a Python File into a Static Executable
How to Convert a Python File into a Static Executable
Follow these steps to convert your Python script (main.py
) into an executable file using PyInstaller:
Step 1: Install PyInstaller
Open your Anaconda Prompt or terminal and run:
pip install pyinstaller
Step 2: Navigate to Your Project Directory
Use the cd
command to go to the folder containing your main.py
file. For example:
cd path/to/your/project
Step 3: Create the Executable
Run the following command to generate a single executable file:
pyinstaller --onefile main.py
Additional options:
--noconsole
: Hides the console window (useful for GUI applications).--icon=icon.ico
: Adds a custom icon to the executable.
Example with additional options:
pyinstaller --onefile --noconsole --icon=myicon.ico main.py
Step 4: Locate the Executable
After running the command, a dist
folder will appear in your project directory. Inside it, you'll find your executable file (main.exe
on Windows).
Step 5: Test the Executable
Double-click the executable file to ensure it runs as expected. If your program relies on additional resources (like files or folders), make sure they are in the same directory as the executable.
Note: PyInstaller packages all required libraries into the executable, so it works on systems without Python installed. Executables are platform-specific. To create executables for other platforms, you’ll need to use the corresponding system or cross-compilation tools.