Categories

Python - Create .exe from .py file (using PyInstaller) - For Windows

To be able to distribute a Python program or Python script without requiring the end user to install Python, the Python code should be converted to executable(.exe) file.

One module that can be used to convert .py files to .exe files is PyInstaller. To use PyInstaller, follow the procedure below:

Installation


1. Download PyInstaller - go to the Download section in the homepage and select the version of PyInstaller

2. Extract the downloaded file 

3. Copy and paste the downloaded pyinstaller folder inside your python folder - inside site-packages folder. (Go to Python26\Lib\site-packages and paste the pyinstaller folder inside site-packages)

4. Download and install pywin32-216.win32-py2.6

5. Download and install gccxml-0.9.0-win32-x86

Creating .exe file

1. Go to the pyinstaller folder(ex. C:\Python26\Lib\site-packages\pyinstaller-1.5.1) and run Configure.py (just double-click the file to run it)

2. Run windows command prompt

3. Change directory to the path of your python folder and go to pyinstaller folder.
    cmd command: cd C:\Python26\Lib\site-packages\pyinstaller-1.5.1

4. Enter this command: Makespec.py --onefile "Directory of your code\code.py"
   For example, the code(with name code.py) is located in drive C:, the command is: Makespec.py --onefile "C:\code.py"

    To change the icon of the .exe file that will be generated, use this command:
       Makespec.py --onefile --icon="Directory of your icon/icon.ico" "Directory of your code\code.py"
    If the icon(icon.ico) is located in drive C also, this will be the command:
    Makespec.py --onefile --icon="C:\icon.ico" "C:\code.py"

5. A folder(name is the same with the code file name without the extension) will be created inside the pyinstaller folder with the .exe file.

No comments:

Post a Comment