Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python 2 path on macOS 12.3+ #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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".


<a name="from-source"></a>
### From source ###

Expand All @@ -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.

<a name="python-2-support-on-macos-123"></a>
### 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 `<plist><dict>`:

```xml
<key>variables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin</string>
devnoname120 marked this conversation as resolved.
Show resolved Hide resolved
</dict>
```

This is required because Alfred doesn't inherit environment variables and doesn't source shell configuration files (such as `.zshrc`).

<a name="usage"></a>
Usage
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README_PYPI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Here's how to show recent `Pinboard.in <https://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``):


Expand Down
2 changes: 1 addition & 1 deletion bin/publish-cheeseshop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
3 changes: 2 additions & 1 deletion docs/guide/background.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
23 changes: 18 additions & 5 deletions docs/guide/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#]_.
Expand All @@ -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
Expand Down Expand Up @@ -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 <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 ``<plist><dict>``:

.. [#] 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.
.. code-block:: xml
:linenos:

<key>variables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin</string>
</dict>

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.
2 changes: 1 addition & 1 deletion docs/guide/text-encoding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Quick example
Here's how to show recent `Pinboard.in <https://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
Expand Down
4 changes: 2 additions & 2 deletions docs/supported-versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial_2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extras/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion extras/benchmarks/00-python-interpreter-only/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

/usr/bin/python -c ''
/usr/bin/env python -c ''
2 changes: 1 addition & 1 deletion extras/benchmarks/01-read-info-plist/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

/usr/bin/python ./script.py
/usr/bin/env python ./script.py
2 changes: 1 addition & 1 deletion extras/benchmarks/01-read-info-plist/script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion extras/benchmarks/02-large-info-plist/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

/usr/bin/python ./script.py
/usr/bin/env python ./script.py
2 changes: 1 addition & 1 deletion extras/benchmarks/02-large-info-plist/script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion extras/benchmarks/03-read-envvars/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion extras/benchmarks/03-read-envvars/script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2014 Dean Jackson <[email protected]>
Expand Down
20 changes: 12 additions & 8 deletions tests/test_workflow_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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']
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion workflow/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions workflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down