1

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" 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!

3
  • @JacobLee See my edit Commented May 15, 2021 at 17:28
  • it still imports all my python modules(more than a hundred) don't worry about that pyinstaller packages everything required for your script to run including the python compiler. If that worries you can use --onefile mode which will package all in a single .exe file. Commented May 16, 2021 at 1:33
  • I already know that, what I mean is that it imports, for instance, Tkinter or matplotlib even though I don't use these libs. Commented May 16, 2021 at 11:49

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer, it doesn't work, check the edit I made to the post to provide more details
Could you add what your imports are? I think that might help understand both the error and the import issues
I use Python 3.8 with : psycopg2, pandas, numpy and PyQt5
There are probably two things going on: First, your 4 modules are importing a lot of other modules, so they must be downloaded too. Second, you might want to try a venv and install only the dependencies you need there. Then, you can make the installer use the venv's installation of python and not your global one.
I already said in my post that I use a venv in which I run the pyinstaller command and it still installs tkinter or matplotlib even though I never use these modules and they are not necessary for the others to work
|
1

Try using auto-py-to-exe, I personally use it. With its gui interface it's really easy to use.

1 Comment

It doesn't work for me, that's why I made this post
0

for Standalone executable --onefile or -F

pyinstaller --noconfirm --onefile --windowed --add-data "config.py;." --add-data "database.ini;." --add-data "images;images/"  "app.py"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.