This short tutorial will show you how to properly install Python 3 on a Mac OS X computer. There are multiple ways to install Python 3, including a download from the official Python site, however I strongly recommend instead using a package manager like Homebrew to manage all your dependencies going forward. It will make your life a lot simpler.
![]() Confirm your Python version
Although Python 2 is installed by default on Apple computers, Python 3 is not. You can confirm this by typing in Terminal
python --version and hitting Enter:
To check if Python 3 is already installed try running the command
python3 --version . Most likely you’ll see an error message, but it’s worth checking. Even if you have a version of Python 3, we want to be on the most recent release, which is 3.7.4 at this point in 2019.
With older versions of Python, there is one Mac OS X quirk that you need to be aware of: programs that talk to the Aqua window manager (in other words, anything that has a GUI) need to be run in a special way. Use pythonw instead of python to start such scripts. With Python 3.3, you can use either python or pythonw.
Install Xcode and Homebrew
We will use the package manager Homebrew to install Python 3. Homebrew depends on Apple’s Xcode package, so run the following command to install it:
Click through all the confirmation commands (Xcode is a large program so this might take a while to install depending on your internet connection).
Next, install Homebrew:
Note: You can also find this command on the homepage of the Homebrew website. It’s easiest to copy and paste since it’s a long command.
To confirm Homebrew installed correctly, run this command:
Install Python 3
To install the latest version of Python, run the following command:
https://evertelecom.weebly.com/blog/microsoft-office-for-mac-update. Now let’s confirm which version was installed:
To open a Python 3 shell from the command line type python3:
When you want to exit, type
exit() and then Return or Ctrl-D (the Control and D key at the same time).
Note you can still run Python shells with Python 2 by simply typing
python : https://evertelecom.weebly.com/blog/svn-clients-for-mac.
Bonus: Virtual Environments
By default, Python packages are installed globally on your computer in a single directory. Which can cause major problems when working on multiple Python projects!
For example, image you have Project A that relies upon Django 1.11 whereas Project B uses Django 2.2. If you naively installed Django on your computer, only the latest install would be present and available in that single directory. Then consider that most Python projects rely on multiple packages that each have their own version numbers. There’s simply no way to keep everything straight and not inadvertently break things with the wrong package versions.
The solution is to use a virtual environment for each project, an isolated directory, rather than installing Python packages globally.
Confusingly, there are multiple tools for virtual environments in Python:
On MacOS we can install Pipenv with HomeBrew.
Python 3 For Machine Learning
Then use Pipenv for any Python packages you wish to install. For example, if you want to work with Django 2.2.6, first create a dedicated directory for it on your computer such as in a django directory on your Desktop.
Then install Django within that directory.
If you look within the directory there are now two new files,
Pipfile and Pipfile.lock , which Pipenv uses. To activate the virtual environment type pipenv shell .
Python 3 For Macbook
There will now be parentheses around the name of your current directory which indicates the virtual environment is activate. To exit the virtual environment, type exit.
Python 3 Mac Terminal
The lack of parentheses confirms the virtual environment is no longer active.
![]()
Want to improve your Python? I have a list of recommended Python books.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |