How to specify Python version and library versions for PyInstaller executable

不问归期 提交于 2021-02-11 08:04:35

问题


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!


回答1:


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).



来源:https://stackoverflow.com/questions/63494904/how-to-specify-python-version-and-library-versions-for-pyinstaller-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!