0

In my Macs Terminal I'm not able to use Python packages as I get a ModuleNotFound error. This is not the case in VS code, as I tried all kinds of things, including a venv based from a tutorial, and it seemed to work.

When I run pip install requests I get the following:

  • Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (2.25.1)
  • Requirement already satisfied: chardet<5,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (4.0.0)
  • Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (1.26.5)
  • Requirement already satisfied: idna<3,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (2.10)
  • Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (2020.12.5)

When I run python --version I get Python 3.9.5

I've spend hours now trying to get this "environment" up and correctly running, but with not much success.

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Nuxurious
  • 79
  • 12
  • Hi, @Nuxurious Mind sharing what exactly do you write in your terminal window? – bot-coder101 May 31 '21 at 20:22
  • Hi @bot-coder101, what I do is I go into Terminal, type in Python which then seems to make the Terminal switch to python, and then I simply type in "import requests" or "import paperclip" -- both which I have installed via Pip and can run in VS code - and then get an error. When I run files via changing directory, and running python randomfile.py with a 3rd party module in it, I also get the error. Not when I run directly in VS code. – Nuxurious May 31 '21 at 20:25
  • Did you try writing a script in a text editor like nano/vim through terminal and then running the script like python3 file.py? Is it working or it gives the same error? – bot-coder101 May 31 '21 at 20:28
  • Hi @9769953, the output I got from that command: ['', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Users/Bas/Library/Python/3.9/lib/python/site-packages', '/usr/local/lib/python3.9/site-packages'] – Nuxurious May 31 '21 at 20:30
  • @bot-coder101 I'm not familiar with those text editors and just started coding, but to add, in IDLE the modules also work with zero problem. – Nuxurious May 31 '21 at 20:31
  • @9769953 strange. I ran the `which python` command in both bash terminals and for both I got `/usr/local/bin/python` – Nuxurious May 31 '21 at 20:36
  • @9769953 my bad. I ran it through the Terminal app and through VS Code menu bar > Terminal > New terminal. I'm not sure how to do it otherwise? – Nuxurious May 31 '21 at 20:44
  • Yes. In VS code, in a .py file, if I use `import requests` or `import pyperclip` it doesn't give an error - and pyperclip works. In Terminal, it does. – Nuxurious May 31 '21 at 20:46
  • It has a shell promo ($) and the interactive shell is zsh. It returns the `/usr/local/bin/python` and when I press enter on that, it starts Python. However, I just ran `import requests` which returned a ModuleNotFoundError. When I just run a .py file with import requests through a Run Python file in Terminal prompt by VS code, it works fine. – Nuxurious May 31 '21 at 20:58
  • Alright, I'll keep that in mind. In the shell it displays, and the script just returned `/Library/Frameworks/Python.framework/Versions/3.9/bin/python3`. I tried to use that as the path in Terminal through some code, today, but perhaps that did not succeed. – Nuxurious May 31 '21 at 21:02
  • Which pip in zsh terminal returned `/Library/Frameworks/Python.framework/Versions/3.9/bin/pip ` – Nuxurious May 31 '21 at 21:03
  • When I ran that path, import requests worked! Now I need to try and make it default, I suppose? – Nuxurious May 31 '21 at 21:04
  • `python3` also worked! – Nuxurious May 31 '21 at 21:06
  • @9769953 I'm still getting some errors. For example, I just switched to my folder where Python scripts are contained (those which work in VS code) and tried to run one including `import requests` and a `print("Hello world")` but it returned a ModuleNotFoundError. Edit: this is in zsh, changing directory to python folder, then running `python file.py` – Nuxurious May 31 '21 at 21:15
  • this is in zsh, changing directory to python folder, then running `python file.py` – Nuxurious May 31 '21 at 21:17
  • Nevermind it's late here. Haha my bad. Ran `python3 file.py` and it worked fine. Thank you for the help! I appreciate it! – Nuxurious May 31 '21 at 21:18
  • @9769953 Many thanks again btw for writing out a very detailed answer! I was able to create an alias for python that goes to python3 which now also works! – Nuxurious May 31 '21 at 21:31

1 Answers1

1

The problem is that you have multiple Python versions installed. One possibly from the official Python website, and one through Homebrew (I'm guessing a bit). These interfere somewhat.

Here's the thing though: the "official" one didn't not install the standard python alias. It only installed the more explicit python3 command. Oddly (and confusingly) enough, it did install pip, probably alongside pip3.

When you type python, the shell will find the one installed by Homebrew; the other one doesn't not exist. When you explicitly type python3, you'll get the correct one. It looks like your PATH environment variable is set up to have the "official" installation come first, and then the Homebrew one.

So, best and easiest is to simply use python3 explicitly.

You could attempt to uninstall the Homebrew one in /usr/local/ if you want to and don't think you need it.

You could also tinker inside the /Library/Frameworks/Python.framework/Versions/3.9/bin and make an alias for python, but I wouldn't recommend it, unless you become more familiar with the shell, Python installations and your system. And after all, it's only one character more to type currently.

9769953
  • 10,344
  • 3
  • 26
  • 37
  • do you get the same thing for `python --version` and `python3 --version`? How about `pip --version`, `python -m pip --version` and `python3 -m pip --version` – Adam Smooch Aug 03 '21 at 18:26