I have a python project that contains multiple python scripts (5), as well as a database.ini file and a folder within, are the images. I looked around on StackOverflow and google to find any way that could allow me to convert this whole project to a folder that contains the dependencies and an executable but I can't find anything that fits what I want to do.
Even though python may not be a good choice to make an "app", I can't do it in another way and I really need to be able to share this project with someone else without him having to install python or any dependencies by himself.
I found that pyinstaller could do it but when I tried, not only I get an error when starting the executable but also, it doesn't read my requirements.txt file and installs a hundred useless python libs.
[EDIT] : I created a virtualenv in the folder that I want to convert and I activated it. Then I installed only the libs that I needed and I ran this command (in the venv): pyinstaller --noconfirm --onedir --windowed --add-data "config.py;." --add-data "database.ini;." --add-data "images;images/" "app.py"
pyinstaller --noconfirm --onedir --windowed --add-data "config.py;." --add-data "database.ini;." --add-data "images;images/" "app.py"
pyinstaller --noconfirm --onedir --windowed --add-data "config.py;." --add-data "database.ini;." --add-data "images;images/" "app.py"
which creates a dist folder and inside /dist/app
I have my.exe but not only it still imports all my python modules (more than a hundred) but also I have the following error: failed to execute script pyi rth win32api
[EDIT 2] : I use Python 3.8 and the only libs used in my whole project are: numpy, pandas, PyQt5 and psycopg2; however pyinstaller also imports matplotlib or tkinter
Thanks!
In case you need to use a screen on which to display, I found the following steps to be useful:
First, make sure that whenever importing files, you use this function instead
def resource_path(relative_path):
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
# example of using it:
img_folder = resource_path('img')
Also, add the following to your code:
import contextlib
with contextlib.redirect_stdout(None):
# importing the modules specified in the --hidden-import part of the command, for example Pygame
import pygame
Then, cd in cmd or powershell into the path with the main file in it and run the following: (Write --hidden-import
for the display module, for example Pygame, and the --onefile
makes it all in one exe so that it's easier to share with friends). python -m PyInstaller --onefile --noconsole Main.py --hidden-import pygame
.
This will create in that same directory two folders and a .spec
file. In the "dist" folder created, you will find your final exe, but it won't be ready yet:
Enter the .spec
file, and go into the datas
section in it - in it, you write which files you import and use in the main file.
For example, you should type something along the lines of datas=[('maps/*','maps'),('img/*','img')],
After that, type python -m PyInstaller Main.spec
in the cmd that's in the same path, and the .exe
file in the "dist" folder.
It's supposed to only import the modules required - if one of the modules you are using has other modules that it itself is using, then it has to download those too.
Try using auto-py-to-exe , I personally use it. With its gui interface it's really easy to use.
for Standalone executable --onefile
or -F
pyinstaller --noconfirm --onefile --windowed --add-data "config.py;." --add-data "database.ini;." --add-data "images;images/" "app.py"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.