Python Forum
[split] Trouble making my first python .exe using pyinstaller.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Trouble making my first python .exe using pyinstaller.
#1
Hi All,

I am facing the below issue when using PyInstaller
Error:
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--add-data <SRC;DEST or SRC:DEST>] [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR] [--hidden-import MODULENAME] [--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES] [--key KEY] [-d] [-s] [--noupx] [-c] [-w] [-i <FILE.ico or FILE.exe,ID or FILE.icns>] [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE] [--uac-admin] [--uac-uiaccess] [--win-private-assemblies] [--win-no-prefer-redirects] [--osx-bundle-identifier BUNDLE_IDENTIFIER] [--runtime-tmpdir PATH] [--distpath DIR] [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL] scriptname [scriptname ...] pyinstaller: error: the following arguments are required: scriptname
I have created a json file to provide the required arguments and a small program to take the data from the json file.
{
	"name": "PythonApp",
	"paths": "",
	"script": "app.py",
	"src_dir": "C:...\\PythonApp",
	"dest_dir": "C:...\\PythonApp\\installation",
	"files": [["core_content/settings.json", "core_content"],
		       ["appConfig.json", "installation"],
		       ["resources.qrc", "installation"]

	],
	"dirs": [["core_content", "core_content"], ["images", "images"], ["log", "log"], ["resources", "resources"], ["ui", "ui"]],


	"cmd": "pyinstaller --onedir --onefile --debug --name={}  --paths=\"{}\" \"{}\""

}
import shutil, errno
import configparser 
import json
import os
import sys
import subprocess



with open("compile.json", "r", encoding="utf-8") as f:
    config = json.load(f)
   
des_dir = config.get("dest_dir")
app_name = config.get("name")
script = config.get("script")
src_dir = config.get("src_dir")
cmd = config.get("cmd")
dirs = config.get("dirs")
files = config.get("files")


if not all([app_name, script, src_dir, des_dir]):
    print("Not all mandatory arguments were given")
    sys.exit(0)
    
if os.path.isdir(des_dir):
    shutil.rmtree(des_dir)
    os.mkdir(des_dir)
os.chdir(des_dir)

script = os.path.join(src_dir, script)

print("Current directory:", os.getcwd())

installer_cmd = cmd.format(app_name, src_dir, script)
print("cmd", installer_cmd)
p = subprocess.Popen(installer_cmd)
p.communicate()


def copy(src, dst):
    try:
        shutil.copytree(src, dst)
    except NotADirectoryError:
        if not os.path.exists(dst):
            os.makedirs(dst)
        shutil.copy(src, dst)


def copy_dirs():
    if not dirs:
        print("No dirs to copy")
    for src, dest in dirs:
        src, dest = os.path.join(src_dir, src), os.path.join(des_dir, "dist", dest)
        copy(src, dest)
    
    
def copy_files():
    if not files:
        print("No files to copy")
    for src, dest in files:
        src, dest = os.path.join(src_dir, src), os.path.join(des_dir, "dist", dest)
        copy(src, dest)

copy_dirs()
copy_files()
shutil.make_archive(app_name, "zip", os.path.join(des_dir, "dist"))


for f in os.listdir("."):
    if f not in (app_name + ".zip"):
        if os.path.isdir(f):
            shutil.rmtree(f)
        elif os.path.isfile(f):
            os.remove(f)
Reply
#2
could you post the exact command you use to make it exe?
it looks like you don't supply the name of your script that you want to convert to exe.
In simplest case (with no options whatsoever) you will go to the folder of your script and run
pyintstaller yourscript.py

see at the bottom of https://www.pyinstaller.org/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I suggest you make your first executable from helloworld.py to verify that you can get PyInstaller to work properly. After that try on your file.

There are two basic ways to run PyInstaller:

a. The Kitchen sink method which is probably not what you want (includes a lot of files):

[b]PyInstaller helloworld.py[/b]

This creates a subfolder named 'helloworld' in the 'dist' subfolder. You must create a zip file of the 'helloworld' subfolder. The zip file is what you distribute.

b. The one file method:

[b]PyInstaller -F helloworld.py[/b]

This creates file 'helloworld.exe' in the 'dist' subfolder. The only file you have to distribute is 'helloworld.exe'.


For more information see the 'Bundling to One Folder' section and the 'Bundling to One File' section in https://pyinstaller.readthedocs.io/en/st...-mode.html

NOTE: I get a Fatal Python error: Py_Initialize: can't initialize sys standard streams when there is a file named abc.py in the same folder as the file being compiled. Either I am doing something wrong or this is a PyInstaller Feature.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
As mention test that all work with Pyinstaller before doing that stuff with reading json an use subprocess.
installer_cmd = cmd.format(app_name, src_dir, script)
print("cmd", installer_cmd)
p = subprocess.Popen(installer_cmd)
p.communicate() 
Will not work without shell=True(unsecure) as you pass in string installer_cmd.
p = subprocess.Popen(installer_cmd, shell=True)
If Pynstaller is in Windows Path,which it will be if Python are in(environment variables Path).
You can call pyinstaller direct no cmd.
This is a test that work,here i use a list so no shell=True.
# sub_run.py
from subprocess import run

out = run(['pyinstaller', '--onefile', 'hello.py'])
# hello.py
print('hello world')
input('press enter to exit')
Test run:
C:\1\sub_run
λ python sub.py
550 INFO: PyInstaller: 3.3.1
550 INFO: Python: 3.6.4
553 INFO: Platform: Windows-10-10.0.16299-SP0
554 INFO: wrote C:\1\sub_run\hello.spec
....... ect
Reply
#5
Message deleted by poster - incorrect posting.
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#6
(Apr-21-2018, 08:42 PM)ljmetzger Wrote: when there is a file named abc.py in the same folder as the file being compiled
There can be problems if name a file abc.py,because of Abstract Base Classes.
So python has a file named abc.py that use this module.
>>> import abc
>>> 
>>> abc.__file__
'C:\\Python36\\lib\\abc.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with installing python domingo251 2 596 Sep-23-2023, 12:03 AM
Last Post: ICanIBB
  Pyinstaller 3 Python 2 mckymntl 0 1,227 Feb-07-2022, 06:28 AM
Last Post: mckymntl
  making variables in my columns and rows in python kronhamilton 2 1,619 Oct-31-2021, 10:38 AM
Last Post: snippsat
  Python 3.7, Windows 10, pyinstaller, winsound, no sound in executable kmarien 3 3,662 Nov-30-2020, 04:10 PM
Last Post: buran
  New to python, having trouble with an exercise Salkay 3 2,154 Feb-18-2020, 01:42 AM
Last Post: Salkay
  Cannot Change Python script to exe using Pyinstaller omar_mohsen 3 2,390 Dec-19-2019, 01:05 PM
Last Post: buran
  More Python Embedding Trouble jibarra 3 2,926 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Making a generalised CSV COPY script in Python Sandy7771989 3 2,431 Jul-05-2019, 11:02 PM
Last Post: Larz60+
  [split] I need help making a four digit code cracker with random tycpytyt 1 2,546 Mar-24-2019, 05:53 PM
Last Post: micseydel
  Error with using pyinstaller to convert python script in Mac OS Takeshio 2 5,374 Oct-19-2018, 02:42 PM
Last Post: Takeshio

Forum Jump:

User Panel Messages

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