Hướng dẫn python not found macos

Hướng dẫn python not found macos

Mac OS X comes with Python 2.7 out of the box.

You do not need to install or configure anything else to use Python 2. These instructions document the installation of Python 3.

The version of Python that ships with OS X is great for learning, but it’s not good for development. The version shipped with OS X may be out of date from the official current Python release, which is considered the stable production version.

Doing it Right¶

Let’s install a real version of Python.

Before installing Python, you’ll need to install GCC. GCC can be obtained by downloading Xcode, the smaller Command Line Tools (must have an Apple account) or the even smaller OSX-GCC-Installer package.

Note

If you already have Xcode installed, do not install OSX-GCC-Installer. In combination, the software can cause issues that are difficult to diagnose.

Note

If you perform a fresh install of Xcode, you will also need to add the commandline tools by running xcode-select --install on the terminal.

While OS X comes with a large number of Unix utilities, those familiar with Linux systems will notice one key component missing: a package manager. Homebrew fills this void.

To install Homebrew, open Terminal or your favorite OS X terminal emulator and run

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

If you have OS X 10.12 (Sierra) or older use this line instead

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Now, we can install Python 3:

This will take a minute or two.

Pip¶

Homebrew installs pip pointing to the Homebrew’d Python 3 for you.

Working with Python 3¶

At this point, you have the system Python 2.7 available, potentially the Homebrew version of Python 2 installed, and the Homebrew version of Python 3 as well.

will launch the Homebrew-installed Python 3 interpreter.

will launch the Homebrew-installed Python 2 interpreter (if any).

will launch the Homebrew-installed Python 3 interpreter.

If the Homebrew version of Python 2 is installed then pip2 will point to Python 2. If the Homebrew version of Python 3 is installed then pip will point to Python 3.

The rest of the guide will assume that python references Python 3.

# Do I have a Python 3 installed?
$ python --version
Python 3.7.1 # Success!

Pipenv & Virtual Environments¶

The next step is to install Pipenv, so you can install dependencies and manage virtual environments.

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

So, onward! To the Pipenv & Virtual Environments docs!


This page is a remixed version of another guide, which is available under the same license.

Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:

Hướng dẫn python not found macos

I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:

Hướng dẫn python not found macos

The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):

Hướng dẫn python not found macos

I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:

  • I deleted and then reinstalled the Python interpreter from python.org.
  • I tried alias python='python3' (which I saw in one of the threads here).
  • I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here).
  • To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again.
  • I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error.

Hướng dẫn python not found macos

asked Mar 23 at 18:02

1

Anyone updating their macOS to Monterey 12.3 will find that they suddenly no longer have the system-provided Python 2.

The reason for this is that Apple removed the system-provided Python 2 installation (details).

So a workaround/solution for this is to use pyenv to install Python 2.7 (or any other specific version you need).

  1. Install pyenv with brew to manage different Python versions: brew install pyenv
  2. List all installable versions with pyenv install --list
  3. Install Python 2.7.18 with pyenv install 2.7.18
  4. List installed versions with pyenv versions
  5. Set global python version with pyenv global 2.7.18
  6. Add eval "$(pyenv init --path)" to ~/.zprofile (or ~/.bash_profile or ~/.zshrc, whichever you need)
  7. Relaunch the shell and check that Python works, or run $ source ~/.zprofile (Thanks masoud soroush!)

answered Mar 29 at 6:30

Bernd KamplBernd Kampl

2,7024 gold badges19 silver badges25 bronze badges

8

OK, after a couple of days trying, this is what has worked for me:

  1. I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much).
  2. I installed python via brew rather than from the official website. It would still return command not found error.
  3. I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3.

Problem solved.

As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.

answered Mar 25 at 17:46

KigKig

1,2651 gold badge3 silver badges13 bronze badges

2

If you simply installed Python 3, just use python3 as the command instead of just python. In my case, I had to install pynev first via Homebrew (executable brew) using brew install pyenv.

But still after using pynev to install Python 2.7.18 and setting it as a global version using pyenv global 2.7.18, I still ran into an error while trying to run python.

What worked for me (since I already had Python 3 installed) was by changing my command to use python3 instead of just python. Of course, this won't be a solution to everyone who may want to use Python 2.

Hướng dẫn python not found macos

answered Apr 13 at 4:32

AlvinAlvin

3891 silver badge7 bronze badges

4

I installed Python then this error occurred and I just run

echo "alias python=/usr/bin/python3" >> ~/.zshrc

and I t worked.on macOS Monterey 12.4, Python 3.8.9

answered Jul 10 at 16:57

Hướng dẫn python not found macos

1

Since you installed python3.9> (https://python.org/downloads/) , you just need to run everything with python3 instead of python. You don't have to downgrade your python to use it... It works fine with suffixing python with 3, i.e. python3 eg. $ which python3, python3 --version, $ python3 script.py etc

answered Aug 3 at 14:51

3

you should try

sudo ln -s /usr/bin/python3 /usr/local/bin/python

that solve my problem:)

answered Jul 24 at 4:17

Hướng dẫn python not found macos

I got zsh: Command not found python after installing using https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg

I solved it by configuring the ENVIRONMENT. I added a line, alias python=/usr/local/bin/python3.10, into the .zshrc file (you must be sure it is the true path).


My macOS (Apple silicon):

Darwin Johns-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64

Hướng dẫn python not found macos

answered Apr 5 at 4:41

Hướng dẫn python not found macos

CheverJohnCheverJohn

251 silver badge6 bronze badges

2

alias python=/usr/bin/python3

This command works but need to re-enter whenever we restart the termian

answered Aug 27 at 19:00

I just had this problem on a new Macbook Pro with macOS Monterey, and the below worked for me using Homebrew.

I don't think aliasing is necessary, or at least it doesn't seem like that when using Pyenv.

  1. Install pyenv (https://github.com/pyenv/pyenv#installation) and its dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment):

    brew install pyenv
    brew install openssl readline sqlite3 xz zlib tcl-tk
    
  2. Install Python 3.10.6 but I assume other 3.x versions should work as well:

    pyenv install 3.10.6
    
  3. Made the necessary changes to my shell according to https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv.

  4. I made the installed Python available everywhere (this can be overridden per project or folder, if necessary):

    pyenv global 3.10.6
    

Now, the output looks like this:

% which python
/Users/jl/.pyenv/shims/python

% python --version
Python 3.10.6

If some scripts still fail, you will want to check that you added Pyenv to all the necessary shell startup files as mentioned in step 3 above.

answered 1 hour ago

I have a MacBook Pro with an Apple M1 chip and macOS v12.4 (Monterey).

  • I deleted all Python versions and cleared the trash
  • Opened Terminal and typed "python"
  • This prompted an xcode.app update
  • Then I downloaded the latest version of Python from python.org (version 2.10.5)
  • Installed that version
  • Quit Terminal
  • Opened Terminal
  • Typed "python3"

It seems to now be there OK.

Hướng dẫn python not found macos

answered Jun 20 at 21:02

StuartStuart

1717 bronze badges

I use brew install brew install , after installation success I want to know the version of python, I run command python --version, and I got the problem zsh: command not found: python.

If you install it successfully, please check in the terminal. It will show you if you got problem in install if not problem you just change command from python --version to python3 --version.

Hướng dẫn python not found macos

Adriaan

17.5k7 gold badges37 silver badges71 bronze badges

answered Jul 21 at 8:26

Hướng dẫn python not found macos

1

Try the command python3 --version if you have installed Python 3.

answered Sep 15 at 4:41

Hướng dẫn python not found macos