Skip to content

File path aruguments not received on macOS #5908

Description

@noonchen

Description of the issue

The app built successfully, however, when I right-click on a file and choose this app to open it, no arguments are received in python script.

However, it works when I manually pass the arguments in Terminal by xxx.app/Contents/MacOS/executable Arg1 Arg2, maybe the kAEOpenDocuments event is not handled correctly?

Context information (for bug reports)

  • Output of pyinstaller --version: 4.3
  • Version of Python: 3.9.5
  • Platform: macOS 11.4
  • How you installed Python: pyenv
  • Did you also try this on another platform? Does it work there?
    This is a macOS-specific issue.

A minimal example program which shows the error

Built a test app using the script & spec file below, right-click on any file and choose test.app to open.

document_event_test.py

#!/usr/bin/env python3
#encoding:utf-8

import sys, os

base = os.path.dirname(sys.argv[0])

with open(os.path.join(base, "logs.txt"), "w+") as f:
    if len(sys.argv) > 1:
        f.write("arguments received:\n")
        for arg in sys.argv[1:]:
            f.write(f"{arg}, is file: {os.path.isfile(arg)}\n")
    else:
        f.write("no arguments received!\n")

spec file:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['document_event_test.py'],
             pathex=[],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          console=False, 
	 icon='')

coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               upx_exclude=[],
               name='test')

app = BUNDLE(coll,
            name='test.app',
            bundle_identifier="test",
            info_plist={
                'NSPrincipalClass': 'NSApplication',
                'NSAppleScriptEnabled': False,
                'CFBundleDocumentTypes': [{
                        'CFBundleTypeName': 'test_file_types',
                        'CFBundleTypeExtensions': ['txt', 'std', 'stdf', 'std*'],
                        'CFBundleTypeRole': 'Viewer'
                        }],
                },
            )

Stacktrace / full error message

A log file is generated in the example above, but the content of the log is always no arguments received!.

Activity

  1. rokm commented on Jun 13, 2021

    @rokm
    Member

    Sounds like #5436, which also has a proof-of-concept patch that collects Apple events in onedir mode into argv before starting the python interpreter.

  2. noonchen commented on Jun 13, 2021

    @noonchen
    Author

    Sounds like #5436, which also has a proof-of-concept patch that collects Apple events in onedir mode into argv before starting the python interpreter.

    Thanks, I'll take a look into that patch. :)

  3. removed
    triagePlease triage and relabel this issue
    on Jun 14, 2021
  4. rokm commented on Jul 24, 2021

    @rokm
    Member

    Re-opening this issue as support for onedir events processing / argv emulation has been reverted for now due to unforeseen side effects with tkinter-based applications.

  5. reopened this on Jul 24, 2021
  6. rokm commented on Aug 19, 2021

    @rokm
    Member

    Support for argv emulation in onedir mode has been re-enabled in the develop branch via #6089, and will be available in the upcoming PyInstaller 5.x series.

    This feature is now disabled by default (in both onefile and onedir mode) and needs to be explicitly opted-in at build time either via --argv-emulation command-line switch or using argv_emulation argument to EXE() in the .spec file.

    This feature also requires bootloader to be rebuilt, so if you want to try using the develop branch until 5.0 is released, you will need to rebuild the bootloader yourself.

  7. locked as resolved and limited conversation to collaborators on Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions