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
exeoutput file has to be able to read from configuration files such asexample.iniorexample.json - Users can edit the application configuration through
example.iniandexample.json - You may find a working example of the end product at
example/dist/selenium-automation-exe.exe - OR you can build your own
exewith the provided.specfile (customizable)
Solutions
Steps to generate exe from scratch for your own python selenium projects
- Make sure pyinstaller and seleniums are installed
# For your other projects
pipenv install pyinstaller
pipenv install selenium
# For this project
pipenv install --dev
-
Download a copy of a
chromedriver.exefrom here and place it in a folder i.e.driver/ -
Run
pyi-makespecwith--onefileoption 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
- Optional: This program reads data from
example.jsonandexample.ini. To make these 2 files customizable by the end users, append code below at the end of your*.specfile. 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.
- Done. You can now run your selenium app at
/dist/selenium-automation-exe.exe
Steps to try out main.py locally without exe
- Make sure
pip,pythonandpipenvare installed
pipenv install --dev
- 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
- Run
pyinstaller
pyinstaller --clean .\selenium-automation-exe.spec
This should generate a dist and a build folder