Pyinstaller: generate -exe file + folder (in --onefile mode)

Viewed 38159

Now i'm working with Pyinstaller. I have an script which get images from a img folder..

/python
|----/img
|----|----icon1.ico
|----|----icon2.ico
|----maint.py

My script to generate .exe is

pyinstaller.py --windowed --noconsole --clean --onefile maint.py

the problem is that only generate the .exe file but the whole folder /img is omitted.

Question: which aditional syntax do I need to put in the previous line in order to get automatically the .exe file + /img folder?

Update 12/18/2013

I mean: that after execution of pyinstaller.py script, with all arguments, I must see in the /dist folder: the .exe file + the /img folder with all icons or bitmaps files I have for my application

Thanks

3 Answers

You can also run pyinstaller within another python script then use shutil.copytree() to move the folders over after. Or use shutil.copyfile() for individual files.

import PyInstaller.__main__
import shutil

PyInstaller.__main__.run([
    'YourProgram.py',
    '--icon=UI/Your_Icon.ico',
    '--onefile',
    '--noconsole',
], )

shutil.copytree('path/', 'dist/path')

You have to solve many issues to get this working. For example:

  • Getting the right resource path
  • Adding data

The first issue is (as mentionned) solved by adjusting paths depending on execution mode.

def app_path(path):
    frozen = 'not'
    if getattr(sys, 'frozen', False):
            # we are running in executable mode
            frozen = 'ever so'
            app_dir = sys._MEIPASS
    else:
            # we are running in a normal Python environment
            app_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(app_dir, path)

For the second issue instead of tree i use the wildcard operator (*) to add what i need.

added_files = [
         ( './pics/*', 'pics' ),
         ( './db/*', 'db' ),
         ]

then in Analysis,

datas = added_files

A thorough answer is quite long. I've written this article to show in some minute details what i went through to solve the issue.

Related