Osx Install Python

/ Comments off

Under the “Python Releases for Mac OS X” heading, click the link for the Latest Python 3 Release - Python 3.x.x. As of this writing, the latest version was Python 3.8.4. Scroll to the bottom and click macOS 64-bit installer to start the download. When the installer is finished downloading, move on to the next step. Step 2: Run the Installer. $ brew switch python 3.7 Error: python does not have a version '3.7' in the Cellar. Python's installed versions: 3.7.61 and restarted the computer, but without success. So how to install python 3.7 as the standard-python-installation on a mac? (Could be also python3.8 by all means). The steps above are mostly based on Maria Mele’s “Install Python 2.7, virtualenv and virtualenvwrapper on OS X Mavericks/Yosemite” Documentation on Python 3 virtual environments Explanation of how Homebrew installs Python — i.e. Why Python 3 isn’t linked to the command`python`, which motivated some of my deviations from the above blog.

Apple’s Mac OS comes with python 2.7 installed by default. Perhaps you may want to use python 3.x.x on your machine and also use pip for package management with python 3.x.x. The easiest way to achieve this is by:

NOTE: This tutorial does not useVirtual Environments like virtualenv or pyenv to manage various python versions

The following command will install the latest version of a module and its dependencies from the Python Packaging Index: python -m pip install SomePackage. For POSIX users (including macOS and Linux users), the examples in this guide assume the use of a virtual environment. For Windows users, the examples in this guide assume that the. Mac OS X has installed python by default, but the version is 2.7.10. It is a little older, so I upgrade python to a newer version ( python 3.7 or python 3.9 ). But after installing the new version, when I open a terminal and run python command, it still uses Python 2.7. This article will tell you how to fix it.

1. Installing python3

  • Follow this link and download the latest python3OS X package
  • Run the package and follow the steps to install python3 on your computer.
  • Once the installation is done, on your Terminal, run

This will print out the version of python installed on your system. The output should be similar to:

You may verify the installation directory of python by runningthe following line on the Terminal.

The prompt should print the install path for python3. An example output is:

2. Install pip3:

  • Securely download the get-pip.py file from this link
  • From the directory where the file was downloaded to, run the following command in the Terminal
  • Once the installation completes you should see the prompt print message similar to this
  • Verify the installation of pip3 by running the following on the Terminal

This should return the install location of pip3.

  • To install python3 packages using pip3, run

Remember to replace packageName with the appropriate package name for your case.

Happy Pythoning!

MacOS comes with Python pre-installed. But it's Python Version 2.7, which is now deprecated (abandoned by the Python developer community).

The entire Python community has now moved on to using Python 3.x (the current version as of writing this is 3.9). And Python 4.x will be out soon, but it will be completely backward compatible.

If you try to run Python from your MacOS terminal, you'll even see this warning:

Until Apple decides to set Python 3.x, as the default you're going to have to install it yourself.

A Single Command to Run Python 3

For some of you reading this, this command may be enough. You can run Python 3 using this command (with the 3 at the end).

If that's all you came for, no worries. Have a fun day and happy coding.

But if you want a proper Python version control system to keep track of various versions – and have fine-grain control over which version you use – this tutorial will show you exactly how to accomplish this.

How to Install Homebrew on Mac

First you need to install Homebrew, a powerful package manager for Mac.

Open up your terminal. You can do this by using MacOS spotlight (command+space) and typing 'terminal'.

Install python on osx

Now that you're in a command line, you can install the latest version of Homebrew by running this command:

Your terminal will ask for Super User-level access. You will need to type your password to run this command. This is the same password you type when you log into your Mac. Type it and hit enter.

Homebrew will ask you to confirm you want to install the following. You have to press enter to continue. (Or press any other key if you get cold feet.)

How to Install pyenv to Manage Your Python Versions

Now let's take a moment to install PyEnv. This library will help you switch between different versions of Python (in case you need to run Python 2.x for some reason, and in anticipation of Python 4.0 coming).

Run this command:

Osx

Now you can install the latest version of Python.

How to Use pyenv to Install Python or Update Your Python Version

Now you just need to run the following command:

Note that you can substitute 3.9.2 for whatever the latest version of Python is. For example, once Python 4.0.0 comes out, you can run this:

Osx install python modules

Troubleshooting pyenv Installation

If you encounter an error that 'C compiler cannot create executables' then the simplest way to solve this is to reinstall Apple's Xcode.

Xcode is a tool created by Apple that includes all the C libraries and other tools that Python uses when it runs on MacOS. Xcode is a whopping 11 gigabytes, but you'll want to be up-to-date. You may want to run this while you're sleeping.

You can get the latest version of Apple's Xcode here. I had to do this after upgrading to MacOS Big Sur, but once I did, all the following commands worked fine. Just re-run the above pyenv install 3.9.2 and it should now work.

How to Set Up Your MacOS PATH for pyenv (Bash or ZSH)

Python

First you need to update your Unix path to pave a way for PyEnv to be able to interact with your system.

Osx Install Python 2.7

This is a long explanation of how PATH works in MacOS (and Unix), straight from the pyenv GitHub repo.

When you run a command like python or pip, your operating system searches through a list of directories to find an executable file with that name. This list of directories lives in an environment variable called PATH, with each directory in the list separated by a colon:
Directories in PATH are searched from left to right, so a matching executable in a directory at the beginning of the list takes precedence over another one at the end. In this example, the /usr/local/bin directory will be searched first, then /usr/bin, then /bin.

And here is their explanation of what a Shim is. I'm quoting them at length again because I really can't explain this better myself.

pyenv works by inserting a directory of shims at the front of your PATH:
Through a process called rehashing, pyenv maintains shims in that directory to match every Python command across every installed version of Python—python, pip, and so on.
Shims are lightweight executables that simply pass your command along to pyenv.

Here's how to update your .bash_profile in Bash (which is installed in MacOS by default):

Or if you've installed ZSH (or OhMyZSH) like I have, you'll want to edit the .zshrc file instead:

Osx install python 3

Then you want to add PyEnv Init to your terminal. Run this command if you're using Bash (again, this is the default with MacOS):

Or run this command if you're using ZSH:

Now reset your terminal by running this command:

How to Set a Version of Python to Global Default (Bash or ZSH)

You can set the latest version of Python to be global, meaning it will be the default version of Python MacOS uses when you run Python applications.

Run this command:

Again, you can replace 3.9.2 with whatever the latest version is.

Now you can verify that this worked by checking the global version of Python:

You should see this output:

The Final Step: Close Your Terminal and Restart it

Osx Install Python Modules

Once you've restarted your browser, you run the python command and you'll launch the new version of Python instead of the old one.

Install Python On Osx

Congratulations. Thank you for reading this, and happy coding.