How do I use UPX with pyinstaller?

39,180

Solution 1

The UPX directory, not UPX executable needs to be specified:

e.g.:

pyinstaller myfile.py --upx-dir=..\upx391w -y --onefile

Solution 2

Adding a new answer, since it appears that PyInstaller is more helpful now (in September 2019) than the current answer and comments suggest.

I see the output 934 INFO: UPX is available. early in the build if UPX is working.

Additionally, I can see many lines of output where PyInstaller is calling upx.

I didn't specify --upx-dir, but did have upx.exe available in my $PATH environment variable.

Solution 3

In addition to GlennS' comment: exact this behaviour is stated in the pyinstaller documentation. so this is not an undocumented accidental benefit in this regard.
See https://pyinstaller.readthedocs.io/en/v3.3.1/usage.html#using-upx

Solution 4

Just install upx

sudo apt install upx

Pyinstaller searches for upx by default unless you specify the flag --noupx.

The above installation will automatically add upx to the $PATH variable and then you do not need to specify --upx-dir flag.

Other way is to download the zip file from upx's repo and use --upx-dir flag.
Refer to @denfromufa's answer.

For windows, the steps might differ.

Share:
39,180
Admin
Author by

Admin

Updated on July 10, 2022

Comments

  • Admin
    Admin almost 2 years

    How do I use UPX with pyinstaller?

    I am following the docs.

    I have downloaded UPX.

    My file looks like:

    import csv
    import selenium
    import pandas
    
    print('Hello')
    

    I then run:

    pyinstaller -F --upx-dir C:\Users\DD\Downloads\upx394w\upx394w\123\upx308w\upx.exe zz.spec
    

    This does not affect the size of the file.

    Any idea how I can get this to work?

    # -*- mode: python -*-
    
    block_cipher = None
    
    
    a = Analysis(['zz.py'],
                 pathex=['C:\\Users\\DA\\13\\14'],
                 binaries=[],
                 datas=[],
                 hiddenimports=[],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher)
    pyz = PYZ(a.pure, a.zipped_data,
                 cipher=block_cipher)
    exe = EXE(pyz,
              a.scripts,
              a.binaries,
              a.zipfiles,
              a.datas,
              name='zz',
              debug=False,
              strip=False,
              upx=True,
              runtime_tmpdir=None,
              console=True )
    
  • Admin
    Admin over 6 years
    This works, though I'm getting "api-ms-win-crt-multibyte-l1-1-0.dll: CantPackException: can't pack new-exe". I've read somewhere that 64 bit support windows is still experimental maybe that is why
  • denfromufa
    denfromufa over 6 years
    maybe you are experiencing this issue? what is your pyinstaller version? github.com/pyinstaller/pyinstaller/issues/1565
  • Admin
    Admin over 6 years
    I got it working. It reduced the size by 700kbs haha, so not a great deal but it worked.
  • Admin
    Admin over 6 years
    Actually I re-tested just now, and its not even doing any upx. Bizzare.
  • Admin
    Admin over 6 years
    Works today. I got a one dir that went from 70mb to 20mb that's as good as 7zip but you can run the exe. pyinstaller myfile.py --upx-dir=C:\upx394w -y --onedir --clean. I added --clean to it so it would stop getting old build. --noupx to test difference.
  • J Jones
    J Jones about 6 years
    @user9062171, how did you figure out which directory to specify for upx? Also, how did you tell if upx was working? I see: 85 INFO: UPX is available., but I don't know how to tell if UPX is actually used...
  • denfromufa
    denfromufa about 6 years
    Just compare the size of executable with and without upx
  • J Jones
    J Jones about 6 years
    there is no difference in the size whether or not I use the --upx-dir option. That's why I'm confused.
  • denfromufa
    denfromufa about 6 years
    that probably means you have achieved minimum entropy of information
  • J Jones
    J Jones about 6 years
    Maybe. It is confusing because the output of Pyinstaller is the same whether I specify --no-upx, --upx-dir, or no specification at all, thus giving me no indication whether or not upx is functioning.
  • Nikhil VJ
    Nikhil VJ about 6 years
    Just for benefits of newbies like me, you have to separately download UPX from github and unzip it somewhere on your system, then write that folder path (absolute or relative) after--upxdir=. Get the latest UPX from github.com/upx/upx/releases .
  • Night Programmer
    Night Programmer over 4 years
    Thank you sir for this code, Now I able to create a file but it doesn't have any extension just simple Hello file is there How I can run this file
  • jahantaila
    jahantaila over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. –