Python Forum
Pyinstaller with Sys.Argv[] - “Failed to Execute Script”?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyinstaller with Sys.Argv[] - “Failed to Execute Script”?
#1
My desperate attempt to find a solution....


I am trying to compile an .EXE on Windows 10 64-bit with PyInstaller. When using the following CMD in Windows CMD prompt, I get a successful execution of the script:

pyinstaller --onefile Mainscript.py
This works fine executing from CMD prompt with arguments (see Python code below). However, the console appears when executing. So, I re-compile to .exe with the following CMD...

pyinstaller --noconsole --onefile Mainscript.py
Now, when running my program via cmd line with arguments, i get "Failed to Execute Script" and i am wondering why.

This occurs due to -w or --onefile i believe. Script runs fine without --onefile.

I call my script and pass arguments via cmd line. 2 of the arguments are paths and i use relative path:
start MainScript.exe "TestPDF.pdf" "TestJPG.jpg" 100
Here is script:

#REQUIRES PDF2IMAGE INSTALLED WITH PIP
#REQUIRES POPPLER BINARIES INSTALLED ON WINDOWS VIA WEBSITE
from pdf2image import convert_from_path
import os.path
import ctypes
import sys

try:
    argcount = len(sys.argv)  #COUNT OF CMD LINE ARGUMENTS
    if argcount != 4:
        if argcount == 1:
            errStr = "No arguments passed"
        else:
            errStr = "Error: incomplete args passed.  " + "Args = " + str(argcount)

            for x in range(1, argcount):  #argv[0] IS ALWAYS THE SCRIPT NAME
                errStr = errStr + str(x) + ": " + sys.argv[x] + "\n"

        ctypes.windll.user32.MessageBoxW(0, errStr, "Python Convert2Thumbnail", 1)
    else:
        inpath = sys.argv[1]
        outpath = sys.argv[2]
        fsize = sys.argv[3]

        pages = convert_from_path(inpath, fsize)
        firstpage = pages[0]
        firstpage.save(outpath, 'JPEG')

except Exception as e:
    errmsg = getattr(e, 'message', repr(e))
    for x in range(1, argcount): 
        errStr = errStr + str(x) + ": " + sys.argv[x] + "\n"
    errStr = errStr + "\n" + "\n" + errmsg
    ctype
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 788 May-02-2023, 08:40 AM
Last Post: Gribouillis
  sys.argv method nngokturk 3 1,082 Jan-23-2023, 10:41 PM
Last Post: deanhystad
  Failed to execute child process (No such file or directory) uriel 1 1,650 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,208 May-05-2022, 12:32 PM
Last Post: H84Gabor
  execute python script guy7200 1 1,590 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,258 May-18-2021, 06:31 AM
Last Post: Skaperen
  import argv Scott 3 5,896 Apr-01-2021, 11:03 AM
Last Post: radix018
  Execute DBCC CHECKDB from python script susja 1 2,099 Aug-12-2020, 02:09 AM
Last Post: susja
  Execute full script samuelbachorik 0 1,537 Aug-06-2020, 08:09 PM
Last Post: samuelbachorik
  Trying to write and execute first Python script garvind25 3 2,889 May-23-2020, 07:36 PM
Last Post: garvind25

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020