20

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 )

6 Answers6

27

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

e.g.:

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

denfromufa
  • 5,610
  • 13
  • 81
  • 138
  • 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 –  Dec 15 '17 at 00:54
  • maybe you are experiencing this issue? what is your pyinstaller version? https://github.com/pyinstaller/pyinstaller/issues/1565 – denfromufa Dec 15 '17 at 16:38
  • I got it working. It reduced the size by 700kbs haha, so not a great deal but it worked. –  Dec 15 '17 at 16:39
  • Actually I re-tested just now, and its not even doing any upx. Bizzare. –  Dec 15 '17 at 17:05
  • 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. –  Dec 16 '17 at 00:36
  • @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... – J Jones Mar 07 '18 at 18:16
  • Just compare the size of executable with and without upx – denfromufa Mar 07 '18 at 18:18
  • there is no difference in the size whether or not I use the --upx-dir option. That's why I'm confused. – J Jones Mar 07 '18 at 21:04
  • that probably means you have achieved minimum entropy of information – denfromufa Mar 07 '18 at 22:09
  • 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. – J Jones Mar 07 '18 at 22:13
  • 11
    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 https://github.com/upx/upx/releases . – Nikhil VJ Apr 24 '18 at 18:53
  • 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 – Night Programmer Oct 09 '19 at 05:33
3

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.

GlennS
  • 5,251
  • 6
  • 28
  • 31
2

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.

Deepam Gupta
  • 2,374
  • 1
  • 30
  • 33
1

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

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
mercury
  • 21
  • 6
1

Check that your script name is correctly spelled. In my case I received the mentioned error because I was using pyinstaller.exe -F .\my_script.py but my script name was my-script.py (notice the '-' vs '_').

Dharman
  • 30,962
  • 25
  • 85
  • 135
ciscology
  • 19
  • 2
  • 1
    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. – – jahantaila Nov 12 '21 at 20:11
1

There might be a problem with UPX as it might not be downloaded so the first step you should do is download UPX over hereUPX official

then once downloaded extract it to a specific folder. There you will find the .exe file so now in the same directory just open cmd and run the command of pyinstaller

pyinstaller --onefile pythonScriptName.py

I hope this works now.

prakhar newatia
  • 169
  • 2
  • 3