Search code examples
pythonpyinstallerexecutablelibrariesinterpreter

How to specify Python version and library versions for PyInstaller executable


When creating an executable file using PyInstaller, how can I bundle a specific Python interpreter and some library from specific versions?

I want to create an executable file from my .py script. My __main__.py downloads some data from a website and transform them, then saves them in a folder as excel files.

And here are requirements.

  1. Use Python 3.6

For this, I found some related posts but never clear to me. My main.py needs Python3.6 to be executed, and the users of this .exe file won't necessarily have Python3.6, so I want to bundle this Python in the .exe file as well. But is just a single command python3 -m pyinstaller __main__.py really enough? I'm not confident and I want to know what exactly I need to do.

  1. Use specific versions of libraries such as pandas==0.23, beautifulsoup4==4.9.1 etc.

I think I need to work with pure in .spec file as the documentation says "pure: pure python modules needed by the scripts". But I cannot find any documentation about what exactly I need to do make it include pandas==0.23, beautifulsoup4==4.9.1 etc.

Any help would be appreciated!


Solution

  • I do this using virtual environments: if you have an environment with the desired Python version and package versions, you can pip install PyInstaller there and build the .EXE from that environment.

    The Conda version would be:

    1. Create the environment:
    conda create --name my_env_name python=3.6
    
    1. Activate it:
    conda activate my_env_name
    
    1. Install your desired package versions and PyInstaller:
    pip install pandas==0.23
    pip install beautifulsoup4==4.9.1
    pip install pyinstaller
    
    # or to get the developer version, which fixes some issues I have run into
    # pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
    
    1. And then build your EXE:
    python pyinstaller myscript.py
    

    Having this virtual environment can be a nice control as well for writing/testing your program, as it ensures no other dependencies are needed. Because of this, I tend to only install the necessary packages for the script into the environment.

    However, I am not sure how one would do this with venv, if you do not have Anaconda. But maybe this will still point you in a direction (or someone else can elaborate? also see this post).