问题
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.
- 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.
- 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:
- Create the environment:
conda create --name my_env_name python=3.6
- Activate it:
conda activate my_env_name
- 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
- 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