Here the answer that worked, which is basically what has been explained in the comments of the question. However, I thought it would be useful to have it explained as a clear and well structured answer.

As highlighted, the problem was that I was not using the interpreter that pip was installing for. The command which shows where pip was installing the modules:

$ which -a pip
/usr/local/bin/pip

and where the different python versions were located:

$ which -a python
/usr/bin/python
/usr/local/bin/python

That is, my system/default python was

/usr/bin/python

while pip was installing for

/usr/local/bin/python

Therefore, I could not import anything I installed when I just typed python, because the /usr/bin/python interpreter was the one started.

Solution

Install pip again specifying the destination of the modules that will be installed. This must be the destination for the system/default python.

This has been done in two steps:

  1. Downloding get-pip.py from bootstrap.pypa.io/get-pip.py. (You may need to use the deprecated one for Python 2: bootstrap.pypa.io/2.7/get-pip.py)

  2. Installing it with the following command

    sudo /usr/bin/python get-pip.py

Note that without the sudo I got an error and was not able to install pip.

Answer from J0ANMM on Stack Overflow
Top answer
1 of 9
27

Here the answer that worked, which is basically what has been explained in the comments of the question. However, I thought it would be useful to have it explained as a clear and well structured answer.

As highlighted, the problem was that I was not using the interpreter that pip was installing for. The command which shows where pip was installing the modules:

$ which -a pip
/usr/local/bin/pip

and where the different python versions were located:

$ which -a python
/usr/bin/python
/usr/local/bin/python

That is, my system/default python was

/usr/bin/python

while pip was installing for

/usr/local/bin/python

Therefore, I could not import anything I installed when I just typed python, because the /usr/bin/python interpreter was the one started.

Solution

Install pip again specifying the destination of the modules that will be installed. This must be the destination for the system/default python.

This has been done in two steps:

  1. Downloding get-pip.py from bootstrap.pypa.io/get-pip.py. (You may need to use the deprecated one for Python 2: bootstrap.pypa.io/2.7/get-pip.py)

  2. Installing it with the following command

    sudo /usr/bin/python get-pip.py

Note that without the sudo I got an error and was not able to install pip.

2 of 9
18

I have just fixed a similar issue.

To give some background, I install pip with homebrew by executing brew install python. One drawback by executing this command, it will install both python2 and python3(maybe not a disadvantage in some case), then

pip install scrapy

but when I try to import scrapy, it complained ImportError: No module named scrapy.


My Solution: run brew doctor, it should report you a link is broken, it asks you to run brew link python, you might encounter some errors, but follow the prompt suggestion to move forward, after successfully executing brew link python, everything should work now.

🌐
Stack Overflow
stackoverflow.com › questions › 64391561 › macos-python-says-module-not-found-but-its-already-installed
MacOS - Python says module not found, but it's already installed - Stack Overflow
So, I did pip3 list to check what modules were currently installed. I saw that ProxyManager was installed but it wasn't the correct module. I typed pip3 uninstall ProxyManager. Then I typed pip3 install proxy-manager.
Discussions

ModuleNotFound error when running .py script on a Mac
Run these two lines at the command line: python -c 'import todoist' python3 -c 'import todoist' and tell me which one of them throws ModuleNotFoundError, or if both of them do. More on reddit.com
🌐 r/learnpython
4
1
July 23, 2020
python - pip installed module doesn't found - macOS - Stack Overflow
Traceback (most recent call last): ... No module named 'watson_developer_cloud' ... I am using macOS Mojave 10.14.2 (beta), pip 10.0.1 (I also tried to upgrade it to 18.1, but after the install the version stayed the same). ... You probably have several python installed, the system one and brew for instance. Make sure you select the proper one all the time. ... Save this answer. ... Show activity on this post. You probably installed it with sudo, so it's not accessible ... More on stackoverflow.com
🌐 stackoverflow.com
Installed Python Module Not Found on macOS - Stack Overflow
I know that this has been asked a number of times before, but none of the solutions I have found are working for me. I think my question is best explained by the below: MacBook-Pro-52:gps_meta_to_csv Thomas$ pip list Package Version ---------- ------- pip 19.1.1 setuptools 40.8.0 wheel 0.33.4 MacBook-Pro-52:gps_meta_to_csv Thomas$ pip3.7 install Pillow Collecting Pillow Using cached https://files.pythonhosted... More on stackoverflow.com
🌐 stackoverflow.com
Py3 OSX "Module not found"
The installation makes no trouble i get the success message for Py2 and 3. When trying to import them Py2 makes no trouble but Py3 says ModuleNotFoundError: No module named 'pyowm'. Install... More on github.com
🌐 github.com
14
September 2, 2017
Top answer
1 of 1
5

It sounds like you've installed a Homebrew Python 2.7, you don't actually need it, and you'd rather not deal with having two conflicting copies of Python 2.7 lying around. In that case, here's what you should do:

  • pip --list > packages.txt. This gets you a list of all the packages you've installed.
  • brew unlink python.
  • sudo easy_install pip && sudo pip install --upgrade setuptools.
  • Open packages.txt in a text editor.
  • sudo pip install [all the packages you want from packages.txt].
    • Or just pip install -r packages.txt if you want all of them.

You may want to consider setting up virtualenv, or a user site-packages directory, so you don't have to keep sudoing everything. Especially if you're used to Homebrew.

Using either brew unlink or brew uninstall will get the extra Python 2.7 out of the way, so only the pre-installed Apple version is accessible, solving your problem. But with brew unlink, it doesn't actually delete anything, so if you change your mind, you can get it back with brew link python. (Or, if you're later sure you never want to go back, you can always brew uninstall python later.)

Also, in any other situation this is the wrong way to get pip, but for the Python versions Apple has distributed with 10.7 through 10.9, it seems to work best. That may change with later versions of setuptools.

Finally, one last pitch: Do you actually need Python 2.7 instead of 3.x? There are all kinds of reasons the answer may be "yes", but if not, you can install a Homebrew 3.4 alongside the Apple 2.7 and they won't conflict at all; you'll get separate programs named python3, python3-config, pip3, ipython3, etc.

🌐
Reddit
reddit.com › r/learnpython › modulenotfound error when running .py script on a mac
r/learnpython on Reddit: ModuleNotFound error when running .py script on a Mac
July 23, 2020 -

So I'm just the end user and don't really know anything about python. The most coding I've ever done is a little HTML, CSS, and JavaScript, although learning python is on my bucket list.

I'm trying to run this Inbox for Todoist Übersicht widget, but I'm just getting an error in the widget: ModuleNotFoundError: No module named 'todoist'. I'm pretty sure the module it's referring to is the one I installed via the pip3 install todoist-python command. From what I can tell, this issue usually comes up when a script running in a virtual environment calls a module outside the virtual environment or something like that. (I'm just parroting what I've read online. I don't know what a virtual environment is.) Most of the fixes I've seen, however, assume that the script calling the module is either another module (what's a module?) being installed via pip (what's pip?) or is part of a project (what's a project?), and so all of the instructions I've seen involve forcing both either into or out of a virtual environment. But since I'm just working with a .coffee file that runs a .sh file that runs a .py file, I don't know how to do any of that.

🌐
GitHub
github.com › csparpa › pyowm › issues › 211
Py3 OSX "Module not found" · Issue #211 · csparpa/pyowm
September 2, 2017 - The installation makes no trouble i get the success message for Py2 and 3. When trying to import them Py2 makes no trouble but Py3 says ModuleNotFoundError: No module named 'pyowm'. Installed via pip2 install pyowm and pip3 install pyowm...
Author: csparpa
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 66909509 › how-to-check-where-a-python-module-is-installed-on-mac
How to check where a python module is installed on mac - Stack Overflow
... You need to make sure that the Python you use to install is also the same Python you use to run your program. You probably installed it using the Python 2 built-in on Macs, then you ran your program with Python 3.
🌐
Swift Forums
forums.swift.org › related projects
Unable to use python modules that are installed on my mac with PythonKit - Related Projects - Swift Forums
May 12, 2024 - So, I'm trying to use pyserial and regular expressions with PythonKit, which I have pip installed onto my mac, but I cannot use it, and it runs into the following error: PythonKit/Python.swift:707: Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'serial' Traceback: File "/Users/username/Library/CloudStorage/OneDrive-Personal/project/projapp/projapp/pycode.py", line 9, in import serial ^^^^^^^^^^^^^ The error found No module name...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-fix-python-installation-errors-on-mac
How to Fix Common Python Installation Errors on macOS
June 10, 2024 - If you're seeing the zsh: command not found: python error, and are certain you have already installed Python, you may need to update the Mac Python PATH.
🌐
Python.org
discuss.python.org › python help
Module not found error with multiple versions of Python on Mac M2 - Python Help - Discussions on Python.org
June 20, 2024 - I am using M2 Pro Sonoma 14.5 and new to Python. Python used to work earlier.Installed another version using brew ,from then python programs does not work. I always get module not found error. Tried uninstalling Homebre…
🌐
Python.org
discuss.python.org › python help
Pygame - Module not found macOS - Python Help - Discussions on Python.org
February 20, 2020 - Hi all, Am trying to add the pygame module on my Mac with pip, but when I try to run my pygame program, it prints that the pygame module couldn’t be found. I’ve tried uninstalling python/reinstalling it. Could it be a …
🌐
Reddit
reddit.com › r/macos › for some reason my macbook is saying python not found even though its installed on my mac please help!!!!!
r/MacOS on Reddit: for some reason my macbook is saying python not found even though its installed on my mac PLEASE HELP!!!!!
May 21, 2022 - That means python is not found in your PATH. Read more about PATH variable, but in short terms you can add export PATH="/path/to/python/bin:$PATH" to your ~/.zshrc (where /path/to/python/bin is a path to bin directory inside 3.6 folder) The ...
🌐
Stack Overflow
stackoverflow.com › questions › 69737385 › python-module-not-found-despite-being-installed
macos - Python Module not found despite being installed - Stack Overflow
I am trying to tell pandoc to use a custom pandocfilter written in python. I am on a MacBook M1 and therefore there is a 2.7 and a homebrewed 3.9.7 version of python installed. For some reason the pandocfilter module is not found despite being installed correctly.
🌐
Super User
superuser.com › questions › 1460732 › python-3-7-4-cant-use-installed-packages-macos
bash - Python 3.7.4 - Cant use installed packages (macOS) - Super User
July 17, 2019 - Don't set your path like shown in the question. python3 and pip3 are in /usr/local/bin, so as long as /usr/local/bin is in front of your path, you're good: ... This should be the default anyway, so if anything is overriding it, please modify ...
🌐
JustAnswer
justanswer.com › computer-programming › fmg56-cannot-use-imports-module-python-suspect-its-not.html
I cannot use imports module in python I suspect its not installed and I am unsure how to install. I have a Mac. I am 11
December 5, 2020 - To fix missing import modules on Mac, first ensure Python and pip are installed by running 'python3 --version' and 'pip3 --version' in Terminal. Use 'pip3 install package_name' to install required modules.