A new old Python
August 24, 2020 at 7:59 AM by Dr. Drang
You may have noticed something new in yesterday’s scripts: the shebang lines were
#!/usr/bin/python3
I’ve been using Python 3 for quite a while, but it’s been a version installed through Anaconda, not one that came from Apple. The reasons are
- Apple didn’t provide a Python 3 until Catalina; and
- I didn’t install Catalina on either of my Macs until this past month.
I intend to keep using the Anaconda-installed version as my regular Python because its environment has all the tools I regularly use in my work: NumPy, SciPy, Pandas, and Matplotlib. But the BitBar scripts were a good way to try out Apple’s Python 3; they needed Python 3’s UTF-8 support1 and didn’t need any of those math/science libraries.
While I said above that Apple provides Python 3 in Catalina, that may be stretching the definition of “provide.” If you look in /usr/bin
, you’ll find something called python3
, but that something may be just a placeholder. If you haven’t installed the Command Line Developer Tools, trying to execute a script via /usr/bin/python3
will get you an error message about an “invalid active developer path.” This happened to me on one of my Macs; presumably, the CLDTs had already been installed on the other Mac and were updated when I switched to Catalina.
If you need to install the CLDTs, this explanation by Flavio Copes of how to do so via the xcode-select
command is clear and concise. Once you’ve done so, you can test your new Python 3 by running
/usr/bin/python3 --version
at the command line. You should get Python 3.7.3
as the response. This was the version released over a year ago, which means its remarkably fresh for an Apple-supplied command-line tool.
If you need to install third-party libraries, as I did with the Mechanize and BeautifulSoup libraries used in my library BitBar script, you’ll have to run the Python 3 version of pip
like this:
/usr/bin/pip3 install mechanize
You’ll probably get a warning that your version of pip
isn’t up to date. As with Python 3 itself, the pip
that comes from Apple is over a year old. It’ll still work.
As I said earlier, I don’t expect to be using Apple’s Python 3 in the future, but it’s nice to see that Mac users can use a modern Python without resort to third-party systems like Homebrew or Anaconda.
-
OK, they didn’t need UTF-8 support, but I did. I’m too old to keep doing the encode/decode dance. ↩