Python Forum

Full Version: [split] Trouble making my first python .exe using pyinstaller.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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/
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
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
Message deleted by poster - incorrect posting.
(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