Skip to content

step by step guide: python3 gooey and pyinstaller

GFoniX edited this page Jan 7, 2023 · 10 revisions

This is a step-by-step guide to release a working portable windows .exe executable from your Python3 script using gooey and pyinstaller. Tested on Windows 10 , gooey v1.0.2 and pyinstaller v3.4 .

1. Install Miniconda3

Download and install miniconda3 from here: https://docs.conda.io/en/latest/miniconda.html , this guide is focusing on python3 so I suggest you to use 'miniconda3' instead of 'miniconda2'.

2. Install gooey and pyinstaller

Open the miniconda3 'Anaconda Power Shell Prompt' from your Applications under the 'Start' menu. And run these lines:

Note: You are now already working inside of your first conda environment called 'base'. Python3 will be already installed by default in the 'base' environment. If you like you can create a new environment for this (see conda documentation), but just using the 'base' default environment will be just fine.

> pip install Gooey 
> pip install pyinstaller

3. Create your building directory and python scripts

Create a folder for your project (for example: c:\mybuildingdir ) and place there your python3 script here. Your python script needs 'argparse' library to manage input parameters (ArgumentParser or GooeyParser function) and 'gooey' library for the graphical interface. This is a minimal example 'test.py' script. It just asks for 3 parameters and prints "Done".

> mkdir c:\mybuildingdir
> cd c:\mybuildingdir
> cat test.py
from argparse import ArgumentParser
from gooey import Gooey
import os
import sys

nonbuffered_stdout = os.fdopen(sys.stdout.fileno(), 'w')
sys.stdout = nonbuffered_stdout

@Gooey(program_name="Mytestprogram")
def parse_args():
    parser = ArgumentParser(description='Add description here')
    parser.add_argument('infile',
                        action='store',
                        default='infile1.txt',
                        help="Input file")
    parser.add_argument('outdir',
                        action='store',
                        default='outdir',
                        help="Output directory")
    parser.add_argument('outfile_name',
                        action='store',
                        default='outfile.txt',
                        help='Output file name')
    args = parser.parse_args()
    return args
	
if __name__ == '__main__':
    conf = parse_args()
    print("Done")

4. Add the build.spec file

Now you need a 'build.spec' file collecting all the informations needed by pyinstaller to build the .exe file. Remember to change these lines according to your script path a = Analysis([r'test.py'], pathex=[r'C:\mybuildingdir'], and this line according your app name name='mytestapp',

> cd c:\mybuildingdir
> cat build.spec
import gooey
gooey_root = os.path.dirname(gooey.__file__)
gooey_languages = Tree(os.path.join(gooey_root, 'languages'), prefix = 'gooey/languages')
gooey_images = Tree(os.path.join(gooey_root, 'images'), prefix = 'gooey/images')

a = Analysis([r'test.py'],
             pathex=[r'C:\mybuildingdir'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None,
             )
pyz = PYZ(a.pure)

options = [('u', None, 'OPTION')]

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          options,
          gooey_languages, 
          gooey_images, 
          name='mytestapp',
          debug=False,
          strip=None,
          upx=True,
          console=True,
          icon=os.path.join(gooey_root, 'images', 'program_icon.ico'))

5. Make sure your script works as expected

Before building the .exe you want to make sure your python script works and your interface appears as you expected. Use the miniconda3 'Anaconda Power Shell Prompt' again to move inside your building directory and execute your python script.

> cd c:\mybuildingdir
> python test.py

6. Build your .exe

Again from the miniconda3 'Anaconda Power Shell Prompt', go inside your building directory and build it!

> cd c:\mybuildingdir
> pyinstaller build.spec

You'll find your windows standalone portable .exe executable inside the 'dist' subfolder. You can just copy this .exe to any other computer without any need of a miniconda or python installed. Enjoy!