From 4018328deeb551885724e9d3277895fbd91f20a5 Mon Sep 17 00:00:00 2001 From: devnoname120 Date: Wed, 23 Mar 2022 21:11:41 +0100 Subject: [PATCH 1/3] Fix Python 2 path on macOS 12.3+ --- README.md | 19 +++++++++++++-- README_PYPI.rst | 2 +- bin/publish-cheeseshop.sh | 2 +- docs/guide/background.rst | 3 ++- docs/guide/setup.rst | 23 +++++++++++++++---- docs/guide/text-encoding.rst | 2 +- docs/guide/variables.rst | 2 +- docs/index.rst | 2 +- docs/supported-versions.rst | 4 ++-- docs/tutorial_1.rst | 2 +- docs/tutorial_2.rst | 2 +- extras/benchmark.py | 2 +- .../00-python-interpreter-only/run.sh | 2 +- extras/benchmarks/01-read-info-plist/run.sh | 2 +- .../benchmarks/01-read-info-plist/script.py | 2 +- extras/benchmarks/02-large-info-plist/run.sh | 2 +- .../benchmarks/02-large-info-plist/script.py | 2 +- extras/benchmarks/03-read-envvars/run.sh | 2 +- extras/benchmarks/03-read-envvars/script.py | 2 +- tests/test_workflow.py | 2 +- tests/test_workflow_update.py | 20 +++++++++------- workflow/background.py | 2 +- workflow/workflow.py | 4 ++-- 23 files changed, 70 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index edca16ce..5cfceddd 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Contents - [Installation](#installation) - [With pip](#with-pip) - [From source](#from-source) + - [Python 2 support on macOS 12.3+](#python-2-support-on-macos-123) - [Usage](#usage) - [Workflow script skeleton](#workflow-script-skeleton) - [Examples](#examples) @@ -96,7 +97,6 @@ You can install any other library available on the [Cheese Shop][cheeseshop] the It is highly advisable to bundle all your workflow's dependencies with your workflow in this way. That way, it will "just work". - ### From source ### @@ -122,6 +122,21 @@ Your workflow should look something like this: Alternatively, you can clone/download the Alfred-Workflow [repository][repo] and copy the `workflow` subdirectory to your workflow's root directory. + +### Python 2 support on macOS 12.3+ ### + +Python 2 was [removed in macOS 12.3](https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes#:~:text=Python%202.7%20was%20removed%20from%20macOS%20in%20this%20update). Users that run macOS 12.3 or newer will have to [install Python 2.7.18 manually](https://www.python.org/downloads/release/python-2718/). +Additionally, the following code needs to be added to the `info.plist` file after ``: + +```xml +variables + + PATH + /usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin + +``` + +This is required because Alfred doesn't inherit environment variables and doesn't source shell configuration files (such as `.zshrc`). Usage @@ -136,7 +151,7 @@ A few examples of how to use Alfred-Workflow. Set up your workflow scripts as follows (if you wish to use the built-in error handling or `sys.path` modification): ```python -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 import sys diff --git a/README_PYPI.rst b/README_PYPI.rst index 4d1ef993..e647dc99 100644 --- a/README_PYPI.rst +++ b/README_PYPI.rst @@ -46,7 +46,7 @@ Here's how to show recent `Pinboard.in `_ posts in Alfred. Create a new workflow in Alfred's preferences. Add a **Script Filter** with -Language ``/usr/bin/python`` and paste the following into the **Script** +Language ``/usr/bin/env python`` and paste the following into the **Script** field (changing ``API_KEY``): diff --git a/bin/publish-cheeseshop.sh b/bin/publish-cheeseshop.sh index f1dea3a2..ecd5fcde 100755 --- a/bin/publish-cheeseshop.sh +++ b/bin/publish-cheeseshop.sh @@ -7,7 +7,7 @@ rootdir=$(cd $(dirname $0)/../; pwd) cd "${rootdir}" version=$( cat workflow/version ) -/usr/bin/python setup.py sdist +/usr/bin/env python setup.py sdist twine upload dist/Alfred-Workflow-$version.tar.gz cd - diff --git a/docs/guide/background.rst b/docs/guide/background.rst index 46441c31..a1013687 100644 --- a/docs/guide/background.rst +++ b/docs/guide/background.rst @@ -39,7 +39,8 @@ background). What we're doing is: # Is cache over 1 hour old or non-existent? if not wf.cached_data_fresh('exchange-rates', 3600): run_in_background('update', - ['/usr/bin/python', + ['/usr/bin/env', + 'python', wf.workflowfile('update_exchange_rates.py')]) if is_running('update'): diff --git a/docs/guide/setup.rst b/docs/guide/setup.rst index d46df8bc..5b0313da 100644 --- a/docs/guide/setup.rst +++ b/docs/guide/setup.rst @@ -20,7 +20,7 @@ following (and only the following) **Escaping** options: The **Script** field should contain the following:: - /usr/bin/python yourscript.py "{query}" + /usr/bin/env python yourscript.py "{query}" where ``yourscript.py`` is the name of your script [#]_. @@ -31,7 +31,7 @@ to capture any errors thrown by your scripts: .. code-block:: python :linenos: - #!/usr/bin/python + #!/usr/bin/env python # encoding: utf-8 import sys @@ -67,7 +67,20 @@ to capture any errors thrown by your scripts: log = wf.logger sys.exit(wf.run(main)) +**Important note**: Python 2 was `removed in macOS 12.3 `_. Users that run macOS 12.3 or newer will have to `install Python 2.7.18 manually `_. +Additionally, the following code needs to be added to the ``info.plist`` file after ````: -.. [#] It's better to specify ``/usr/bin/python`` over just ``python``. This - ensures that the script will always be run with the system default - Python regardless of what ``PATH`` might be. \ No newline at end of file +.. code-block:: xml + :linenos: + + variables + + PATH + /usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin + + +This is required because Alfred doesn't inherit environment variables and doesn't source shell configuration files (such as `.zshrc`). + + +.. [#] It's better to specify ``/usr/bin/env python`` over just ``python``. This + ensures that the script will always be found. \ No newline at end of file diff --git a/docs/guide/text-encoding.rst b/docs/guide/text-encoding.rst index 57616343..5abc5817 100644 --- a/docs/guide/text-encoding.rst +++ b/docs/guide/text-encoding.rst @@ -66,7 +66,7 @@ Use :meth:`Workflow.decode` to decode input and .. code-block:: python :linenos: - #!/usr/bin/python + #!/usr/bin/env python # encoding: utf-8 # Because we want to work with Unicode, it's simpler if we make diff --git a/docs/guide/variables.rst b/docs/guide/variables.rst index b4378d1e..13880f9c 100644 --- a/docs/guide/variables.rst +++ b/docs/guide/variables.rst @@ -190,7 +190,7 @@ read: .. code-block:: bash - /usr/bin/python myscript.py pages + /usr/bin/env python myscript.py pages The other options (``--view``, ``--edit``, ``--share``) are set via the diff --git a/docs/index.rst b/docs/index.rst index f2b064d2..a956baae 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -58,7 +58,7 @@ Quick example Here's how to show recent `Pinboard.in `_ posts in Alfred. Create a new workflow in Alfred's preferences. Add a **Script Filter** with -Language ``/usr/bin/python`` and paste the following into the **Script** +Language ``/usr/bin/env python`` and paste the following into the **Script** box (changing ``API_KEY``): .. code-block:: python diff --git a/docs/supported-versions.rst b/docs/supported-versions.rst index efcced9d..885149e9 100644 --- a/docs/supported-versions.rst +++ b/docs/supported-versions.rst @@ -51,7 +51,7 @@ Alfred-Workflow supports the same macOS versions as Alfred, namely 10.6 (Snow Le Python versions =============== -Alfred-Workflow only officially supports the system Pythons that come with macOS (i.e. ``/usr/bin/python``), which is 2.6 on 10.6/Snow Leopard and 2.7 on later versions. +Alfred-Workflow only officially supports the system Pythons that come with macOS (i.e. ``/usr/bin/env python``), which is 2.6 on 10.6/Snow Leopard and 2.7 on later versions. .. important:: @@ -79,7 +79,7 @@ Here is the `full list of new features in Python 2.7`_, but the most important t - No set literals. - No dictionary or set comprehensions. -Python 2.6 is still included in later versions of macOS (up to and including El Capitan), so run your Python scripts with ``/usr/bin/python2.6`` in addition to ``/usr/bin/python`` (2.7) to make sure they will run on Snow Leopard. +Python 2.6 is still included in later versions of macOS (up to and including El Capitan), so run your Python scripts with ``/usr/bin/env python2.6`` in addition to ``/usr/bin/env python`` (2.7) to make sure they will run on Snow Leopard. Why no Python 3 support? diff --git a/docs/tutorial_1.rst b/docs/tutorial_1.rst index a0643a09..fbf34329 100644 --- a/docs/tutorial_1.rst +++ b/docs/tutorial_1.rst @@ -79,7 +79,7 @@ argument. .. note:: - You *can* choose ``/usr/bin/python`` as the **Language** and paste + You *can* choose ``/usr/bin/env python`` as the **Language** and paste your Python code into the **Script** box, but this isn't the best idea. If you do this, you can't run the script from the Terminal (which can be diff --git a/docs/tutorial_2.rst b/docs/tutorial_2.rst index 0dc47521..d3f46604 100644 --- a/docs/tutorial_2.rst +++ b/docs/tutorial_2.rst @@ -892,7 +892,7 @@ update itself: # Start update script if cached data are too old (or doesn't exist) if not wf.cached_data_fresh('posts', max_age=600): - cmd = ['/usr/bin/python', wf.workflowfile('update.py')] + cmd = ['/usr/bin/env', 'python', wf.workflowfile('update.py')] run_in_background('update', cmd) # Notify the user if the cache is being updated diff --git a/extras/benchmark.py b/extras/benchmark.py index 7f1c96f0..c9298fcc 100755 --- a/extras/benchmark.py +++ b/extras/benchmark.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2016 Dean Jackson diff --git a/extras/benchmarks/00-python-interpreter-only/run.sh b/extras/benchmarks/00-python-interpreter-only/run.sh index 0172a6f5..7e0d39a3 100755 --- a/extras/benchmarks/00-python-interpreter-only/run.sh +++ b/extras/benchmarks/00-python-interpreter-only/run.sh @@ -1,3 +1,3 @@ #!/bin/bash -/usr/bin/python -c '' +/usr/bin/env python -c '' diff --git a/extras/benchmarks/01-read-info-plist/run.sh b/extras/benchmarks/01-read-info-plist/run.sh index af73361e..1c31b3bd 100755 --- a/extras/benchmarks/01-read-info-plist/run.sh +++ b/extras/benchmarks/01-read-info-plist/run.sh @@ -1,3 +1,3 @@ #!/bin/bash -/usr/bin/python ./script.py +/usr/bin/env python ./script.py diff --git a/extras/benchmarks/01-read-info-plist/script.py b/extras/benchmarks/01-read-info-plist/script.py index fc563a87..b93d6e4a 100644 --- a/extras/benchmarks/01-read-info-plist/script.py +++ b/extras/benchmarks/01-read-info-plist/script.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2016 Dean Jackson diff --git a/extras/benchmarks/02-large-info-plist/run.sh b/extras/benchmarks/02-large-info-plist/run.sh index af73361e..1c31b3bd 100755 --- a/extras/benchmarks/02-large-info-plist/run.sh +++ b/extras/benchmarks/02-large-info-plist/run.sh @@ -1,3 +1,3 @@ #!/bin/bash -/usr/bin/python ./script.py +/usr/bin/env python ./script.py diff --git a/extras/benchmarks/02-large-info-plist/script.py b/extras/benchmarks/02-large-info-plist/script.py index fc563a87..b93d6e4a 100644 --- a/extras/benchmarks/02-large-info-plist/script.py +++ b/extras/benchmarks/02-large-info-plist/script.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2016 Dean Jackson diff --git a/extras/benchmarks/03-read-envvars/run.sh b/extras/benchmarks/03-read-envvars/run.sh index 38eb63cf..11b46423 100755 --- a/extras/benchmarks/03-read-envvars/run.sh +++ b/extras/benchmarks/03-read-envvars/run.sh @@ -7,4 +7,4 @@ export alfred_workflow_data="$HOME/Library/Application Support/Alfred 2/Workflow # export alfred_workflow_name="Alfred Workflow" # export alfred_workflow_version="1.1.1" -/usr/bin/python ./script.py +/usr/bin/env python ./script.py diff --git a/extras/benchmarks/03-read-envvars/script.py b/extras/benchmarks/03-read-envvars/script.py index fc563a87..b93d6e4a 100644 --- a/extras/benchmarks/03-read-envvars/script.py +++ b/extras/benchmarks/03-read-envvars/script.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2016 Dean Jackson diff --git a/tests/test_workflow.py b/tests/test_workflow.py index 626fa667..3237416a 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2014 Dean Jackson diff --git a/tests/test_workflow_update.py b/tests/test_workflow_update.py index 72485fe3..5c8c105b 100644 --- a/tests/test_workflow_update.py +++ b/tests/test_workflow_update.py @@ -91,15 +91,17 @@ def update(wf): with fakeresponse(httpserver, RELEASES_JSON, HTTP_HEADERS_JSON): with ctx() as (wf, c): wf.run(update) - assert c.cmd[0] == '/usr/bin/python' - assert c.cmd[2] == '__workflow_update_check' + assert c.cmd[0] == '/usr/bin/env' + assert c.cmd[1] == 'python' + assert c.cmd[3] == '__workflow_update_check' update_settings = UPDATE_SETTINGS.copy() update_settings['prereleases'] = True with ctx(update_settings=update_settings) as (wf, c): wf.run(update) - assert c.cmd[0] == '/usr/bin/python' - assert c.cmd[2] == '__workflow_update_check' + assert c.cmd[0] == '/usr/bin/env' + assert c.cmd[1] == 'python' + assert c.cmd[3] == '__workflow_update_check' def test_install_update(httpserver, alfred4): @@ -116,8 +118,9 @@ def fake(wf): print('Magic update command : {0!r}'.format(c.cmd)) - assert c.cmd[0] == '/usr/bin/python' - assert c.cmd[2] == '__workflow_update_install' + assert c.cmd[0] == '/usr/bin/env' + assert c.cmd[1] == 'python' + assert c.cmd[3] == '__workflow_update_install' update_settings = UPDATE_SETTINGS.copy() del update_settings['version'] @@ -164,8 +167,9 @@ def fake(wf): print('Magic update command : {!r}'.format(c.cmd)) - assert c.cmd[0] == '/usr/bin/python' - assert c.cmd[2] == '__workflow_update_install' + assert c.cmd[0] == '/usr/bin/env' + assert c.cmd[1] == 'python' + assert c.cmd[3] == '__workflow_update_install' with env(alfred_workflow_version='v10.0-beta'): update_settings = UPDATE_SETTINGS.copy() diff --git a/workflow/background.py b/workflow/background.py index c2bd7352..978417ad 100644 --- a/workflow/background.py +++ b/workflow/background.py @@ -230,7 +230,7 @@ def run_in_background(name, args, **kwargs): _log().debug('[%s] command cached: %s', name, argcache) # Call this script - cmd = ['/usr/bin/python', __file__, name] + cmd = ['/usr/bin/env', 'python', __file__, name] _log().debug('[%s] passing job to background runner: %r', name, cmd) retcode = subprocess.call(cmd) diff --git a/workflow/workflow.py b/workflow/workflow.py index 39352279..73132935 100644 --- a/workflow/workflow.py +++ b/workflow/workflow.py @@ -2330,7 +2330,7 @@ def check_update(self, force=False): update_script = os.path.join(os.path.dirname(__file__), b'update.py') - cmd = ['/usr/bin/python', update_script, 'check', repo, version] + cmd = ['/usr/bin/env', 'python', update_script, 'check', repo, version] if self.prereleases: cmd.append('--prereleases') @@ -2369,7 +2369,7 @@ def start_update(self): update_script = os.path.join(os.path.dirname(__file__), b'update.py') - cmd = ['/usr/bin/python', update_script, 'install', repo, version] + cmd = ['/usr/bin/env', 'python', update_script, 'install', repo, version] if self.prereleases: cmd.append('--prereleases') From d219f08029de601935661f81c5b00f77486c29fc Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 21 May 2022 10:05:20 +0200 Subject: [PATCH 2/3] Add `/opt/homebrew/bin` to `PATH` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cfceddd..f6ba8e4e 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Additionally, the following code needs to be added to the `info.plist` file afte variables PATH - /usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin + /usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/homebrew/bin ``` From 3090f0c87601565679cfbe52d222615e1d11dbb2 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 22 May 2022 10:57:24 +0200 Subject: [PATCH 3/3] =?UTF-8?q?README=20=E2=80=94=20Include=20default=20pa?= =?UTF-8?q?ths=20in=20`PATH`=20variable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to @vitorgalvao --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f6ba8e4e..9d0e857d 100644 --- a/README.md +++ b/README.md @@ -132,11 +132,11 @@ Additionally, the following code needs to be added to the `info.plist` file afte variables PATH - /usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/homebrew/bin + /Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/usr/local/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin ``` -This is required because Alfred doesn't inherit environment variables and doesn't source shell configuration files (such as `.zshrc`). +This is required because Alfred doesn't inherit environment variables and doesn't source shell configuration files (such as `.zshrc`). By default, the `PATH` variable is [set to `/usr/bin:/bin:/usr/sbin:/sbin`](https://www.alfredapp.com/help/workflows/advanced/understanding-scripting-environment/) by Alfred. Usage