Skip to content

Instantly share code, notes, and snippets.

@Quar
Last active December 19, 2017 00:55
Show Gist options
  • Save Quar/821c9f21b664b08a9b1421d6813e3aa1 to your computer and use it in GitHub Desktop.
Save Quar/821c9f21b664b08a9b1421d6813e3aa1 to your computer and use it in GitHub Desktop.
Portable Python
MAIN ?= $(firstword $(wildcard *.py))
CONDA_ENV ?= "$(shell conda info --envs | sed -n 's/^\([^[:space:]]*\).*\*.*/\1/p')"
SHELL = /bin/bash
LD_LIBRARY_PATH := "$(shell conda info --envs | sed -n 's/.*\*[ ]*\(\/.*\)$$/\1/p')/lib/"
SET_CONDA = source activate $(CONDA_ENV)
.PHONY: help
help: $(firstword $(MAKEFILE_LIST))
@sed -n 's/^##HELP//p' $^
@echo
##HELP exe [MAIN=main.py] [CONDA_ENV=other_conda_env]
##HELP
##HELP MAIN -- path to main entrance python script, default to the
##HELP first '*.py' file in current directory
##HELP CONDA_ENV -- conda environment to run, default to
##HELP current conda environment
.PHONY: exe
exe:
@echo "==> export LD_LIBRARY_PATH=$(LD_LIBRARY_PATH)"
@echo "==> Use conda env $(CONDA_ENV)"
$(SET_CONDA) && LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) pyinstaller -F $(MAIN)
##HELP pandas [MAIN=main.py] [CONDA_ENV=other_conda_env]
##HELP Python Scripts imported panads need special attention, see PyInstaller #2978
##HELP
##HELP MAIN -- path to main entrance python script, default to the
##HELP first '*.py' file in current directory
##HELP CONDA_ENV -- conda environment to run, default to
##HELP current conda environment
.PHONY: pandas
pandas:
@echo "==> export LD_LIBRARY_PATH=$(LD_LIBRARY_PATH)"
@echo "==> Use conda env $(CONDA_ENV)"
$(SET_CONDA) && LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) pyinstaller -F --hidden-import=pandas._libs.tslibs.timedeltas $(MAIN)

Use pyintaller with Conda

  1. install from conda-forge
conda install -c conda-forge pyinstaller
  1. compile into one fat executable
# make sure activate conda env first
# source activate root
LD_LIBRARY_PATH="$(conda info --envs | sed -n 's/.*\*[ ]*\(\/.*\)$/\1/p')/lib/" pyinstaller -F <your_python_script>
  1. checkout generated executable in dist/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment