Skip to content

Instantly share code, notes, and snippets.

@iamvickyav
Last active September 7, 2024 08:06
Show Gist options
  • Save iamvickyav/d3740755597e6d0747462cff568ee3e1 to your computer and use it in GitHub Desktop.
Save iamvickyav/d3740755597e6d0747462cff568ee3e1 to your computer and use it in GitHub Desktop.

Convert Python as .exe file with pandas dependency in Windows

Installation

Create following code inside app.py file inside a folder

# app.py
import pandas as pd

data = {
  "calories": [420, 430, 440],
  "duration": [50, 30, 20]
 }

df = pd.DataFrame(data)
print(df)

Run following commands

> pip install virtualenv

> .\venv\bin\activate

# If you face any issue in executing above command (E.g. activate.psl cannot be loaded because running scripts is disabled on this system
> Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

# Run the command again
>  .\venv\bin\activate

#  do not use conda to install dependencies. only use pip
> pip install pandas
> pip install pyinstaller

# Check if the program is working as expected

> python.exe .\app.py

# Time to package as .exe

# Below command will create `app.spec` file. We need to edit it
> pyi-makespec --onefile .\app.py

Editing app.spec file

Add pandas under hiddenimports section

# -*- mode: python ; coding: utf-8 -*-

a = Analysis(['app.py'],
             pathex=[''],
             binaries=[],
             datas=[],
             hiddenimports=['pandas'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             noarchive=False,
             optimize=0,)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.datas,
          [],
          name='app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True,
          disable_windowed_traceback=False,
          argv_emulation=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None,)

Continue creating .exe file

# pyinstaller < 5.0

> pyinstaller -F app.spec

# If pyinstaller >= 5.0

> pyinstaller app.spec

# Above commands should have created .exe file under dist folder. You can execute & test it like below

> cd dist
> .\app.exe

Thats all folks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment