Converting a python program which uses local files to .exe

Viewed 203

I am trying to convert my python code to an exe file I have these 4 files enter image description here

Using google I found a way by using pyinstaller main.py but that is only for a single file but in my code, I am using a local file which later on I have to add in the folder where my .exe file is but is there any way I can combine them and share it for people to use it

enter image description here

1 Answers

You have to build your .exe with pyinstaller main.py -F --add-data "ch.jpg;." .... You can specify --add-data multiple times, so for each file you want to include.

Then internally you can access your file like this:

import os
from sys import _MEIPASS

open(os.path.join(sys._MEIPASS, "ch.jpg"), "r")

Basically _MEIPASS is a directory where this included files will be created when starting you application.

The syntax for --add-data is like that: file_name;the_real_directory.

So you can access the file by the name file_name, the_real_directory describes where the file actually is when building your .exe. But it's more complex then that, see the docs for more.

EDIT: Also a .spec file is generated when you first build. You can look in there and edit Analysis(datas=[]) to add all your files.

Related