0

I have a hard time understanding how Python is installed on my Mac. Not sure if it is important, but I have a Macbook Pro with an M1 Pro chip and come from years of using Linux with a package-manager and thus mostly use iTerm 2 together with brew to manage my installations.

When I open up iTerm and enter the command python3 I am greeted with

❯ python3
Python 3.8.9 (default, Oct 26 2021, 07:25:53)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> 

I can import packages that I have installed with pip3. But recently I installed PyQt5 over the command brew install pyqt5 and as a dependency, it installed Python 3.9.9 alongside it. Now I can also use the command python 3.9 and I get

❯ python3.9
Python 3.9.9 (main, Nov 21 2021, 03:16:13)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>

I can see that 3.8.9 is the default, which I find a bit weird (I would have expected that the newest stable release version gets used as default), but I don't understand at all why the packages I installed with pip3 can be imported by python3.8.9 but not by python3.9.9... I'm hesitant to remove any versions of python because I know that macOS depends on some of them... Could somebody shed some light on what is going on here and how I can set the newest version as the default version and get it to recognize the installed packages?

2 Answers 2

3

You have two separate installations of python3 and they know nothing of each other,

One from Apple the original in /usr/bin/python3 the 3.8.9 version

The other via Homebrew (I suspect in /opt/homebrew/python3) the 3.9.9

They know nothing of each other.

pip3 is a script file that is related to the python version it works with - the first line of pip3 is #!<path>/python3

To use the newer homebrew version you need pip3 from Homebrew and then uses that pip3 to install the python packages you installed for the Apple version.

The version of python (and pip) you end up using is the version that is first on your $PATH - see which python3 so see what the shell thinks

If you really want to use python you should also look at the official python way of using multiple python versions and package versions - venv https://docs.python.org/3/library/venv.html

4
  • Not sure that the official way is best and easy on macOS. After getting in a tangle some years ago, I just use brew python3 and pip3 and nothing else. Keeping my $PATH in order can be a nightmare!
    – Gilby
    Jan 8, 2022 at 21:56
  • Python and env has changed over the years so a few years ago it might have been a mess
    – mmmmmm
    Jan 8, 2022 at 23:09
  • Well, the "best" way is to not involve Homebrew at all, and just use the official installer. Homebrew will randomly replace the running version of Python with something else. Jan 9, 2022 at 0:40
  • Also see: Homebrew Python Is Not For You Jan 22, 2022 at 9:19
0

Because each of them has their own site-packages directory. It's how Python works, after all. They don't share.

The Python 3.8.9 you have installed is the one provided by Apple through the CLT.

The Python 3.9.9 you have installed is the one from Homebrew. If you want to install packages in it, do so.

Homebrew probably has some broken method to override python3.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .