Problem Statement

Setting up a Python project can be frustrating for non-developers. From downloading the right version of python, setting up virtual environment to installing dependencies.

pyinstaller helps to abstract all these by bundling all your dependencies together. This project demonstrates:

  • A way to distribute any selenium Python applications to non-developers through an executable
  • Also the exe output file has to be able to read from configuration files such as example.ini or example.json
  • Users can edit the application configuration through example.ini and example.json
  • You may find a working example of the end product at example/dist/selenium-automation-exe.exe
  • OR you can build your own exe with the provided .spec file (customizable)

Solutions

Steps to generate exe from scratch for your own python selenium projects

  1. Make sure pyinstaller and seleniums are installed

# For your other projects
pipenv install pyinstaller
pipenv install selenium

# For this project
pipenv install --dev
  1. Download a copy of a chromedriver.exe from here and place it in a folder i.e. driver/

  2. Run pyi-makespec with --onefile option to create a single executable file for easy distribution

pyi-makespec main.py --onefile --noconsole --add-binary "driver\chromedriver.exe;driver\" --add-data "example.json;." --add-data "example.ini;." --name selenium-automation-exe --icon=favicon.ico --console
  1. Optional: This program reads data from example.json and example.ini. To make these 2 files customizable by the end users, append code below at the end of your *.spec file. Example
import shutil
shutil.copyfile('example.ini', '{0}/example.ini'.format(DISTPATH))
shutil.copyfile('example.json', '{0}/example.json'.format(DISTPATH))

Then, run pyinstaller --clean .\selenium-automation-exe.spec again. You should see example.ini and example.json inside dist folder.

  1. Done. You can now run your selenium app at /dist/selenium-automation-exe.exe

Steps to try out main.py locally without exe

  1. Make sure pip, python and pipenv are installed
pipenv install --dev
  1. Run the command below to start the selenium script
# Activate virtualenv
pipenv shell

python main.py

Steps to generate exe file with this repository's .spec file

  1. Run pyinstaller
pyinstaller --clean .\selenium-automation-exe.spec

This should generate a dist and a build folder

GitHub

https://github.com/ngshiheng/selenium-automation-app