Create images and some configuration files like txt and xml files with pyinstaller

I have a problem with pyinstaller when adding some data files, such as images and txts, to a python program. For example, I created a toolbar with icons, and I store them in a separate folder with images. I also have some txt and xml files for storing some configurations. These txt ant xml files are stored in another folder named data_files. All py files are stored in another folder named source.

When I try to create this python program using pyinstaller, pyinstaller can successfully create the entire source folder, but image folders and data_files cannot be created. I checked the pyinstaller documentation, but I could not find a solution. I did a lot of Google searches and found only this resource as useful, but it is a very inadequate resource.

My main question is: how can I create an exe file with a separately created folder, such as images and confs, using pyinstaller?

Edit: I found a solution. When you create the specification file, you must have the a.datas parameter.

# -*- mode: python -*- a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), '/home/vmode/Desktop/derlem2/sources/TextSTAT.py'], pathex=['/home/pyinstaller']) a.datas += [("Images/New.gif","/home/vmode/Desktop/derlem2/Images/New.gif","DATA")] a.datas += [("Images/Open.gif","/home/vmode/Desktop/derlem2/Images/Open.gif","DATA")] a.datas += [("Images/Column.gif","/home/vmode/Desktop/derlem2/Images/Column.gif","DATA")] pyz = PYZ(a.pure) exe = EXE( pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name=os.path.join('dist', 'TextSTAT'), debug=False, strip=False, upx=True, console=1 ) 

The above specification code is for my project. As you can see from this specification code, I have included 3 icons to create using the python program. When the program is generated, there is a separate executable file with built-in icons.

But what if we have so many files? Suppose we have 1000 icons to use in our program. It is very inadequate to write to the specification file manually. To read the file directory there must be a loop system and dynamically add these files. But I could not find how to add these files dynamically. I think there is no chance to add these so many files. If anyone knows, please share.

+8
python pyinstaller
source share
2 answers

When creating my executable, I ran into this problem. Since the .spec file uses python, the glob module was my solution.

 imagesList = [] imagesList.append( ('icon.ico', '..\\pathfinder\\icon.ico', 'DATA') ) import glob allImages = glob.glob('..\\pathfinder\\*.png') for eachImage in allImages: imageParts = eachImage.split('\\') imagesList.append( (imageParts[-1], eachImage, 'DATA') ) print imagesList a.datas += imagesList 

Line 2 relative to icon.ico was separate because it had a different file extension and therefore could be excluded. '.. \ pathfinder' is the path for all my program files (relative to the pyinstaller directory). glob looks for a directory for all .png files (I have 65 and I refused to write them manually) and returns a list of them in the form of strings, the full relative directory is intact. To split it into a list of file names, I used string.split () for the backslash. Then, for each, add a tuple to the user list of the final part of the line split (which will ultimately be just the file name) and the full path name. Finally, with a list of all the files, add it to a.datas.

I confirmed on the command line that this really works correctly, and you can re-copy the loop for any additional file extensions.

Unfortunately, even though it seems to correctly pull and attach file names, my executable is still not created with the files (or the executable cannot find the files in its own data), and I still need to have the images in the same directory for it to work; maybe I have tuple elements ordered incorrectly (and therefore, if so, you should adjust my code accordingly). Hopefully this dynamic load will work for you anyway. imageParts [-1] represents each .png file name, and eachImage represents its full path (or relative path, if that is what was used in glob).

+5
source share

If all images are listed in one directory, you can use Tree

 dict_tree = Tree('/home/vmode/Desktop/derlem2/Images', prefix = 'Images') a.datas += dict_tree 
+1
source share

All Articles