This article will help you to set up a Python virtual environment on Mac OS or your Macbook. If you are a Mac user, you should know that Python 2.7.x comes pre-installed in your Macbook, but as that is required by your operating system, so you cannot modify it or update it and I would recommend that you don't use it at all.
Instead, you should set up a virtual environment for your development purpose. You must be thinking about why we need a virtual environment? So here are a few points in its favor:
Check the new method below for preferred installation! Easyinstall has been deprecated. Please use get-pip.py instead. Step #1 — First of all download the get-pip file by running: curl -o get-pip.py. Step #2 — Now run this file to install pip. Python get-pip.py. That should do it. Maria Campbell provides a post on installing the latest version of Python on Mac OS Catalina and overriding the old default pre-installed version. And it even uses Homebrew and not some gnarly steps. I finally did it. I successfully installed Python version 3.7.7 via Homebrew on my Mac laptop with OS Catalina installed.
When we have a virtual environment, then we can install new packages inside the virtual environment which will not affect the operating system's python modules.
We can have different versions of Python installed inside the virtual environment.
You can set up a different virtual environment for each project for example if you are working on one project based on Tkinter module, some other projects based on Numpy module, then you can easily do this.
So to set up a virtual environment, we won't be using virtualenv or virtualenvwrapper module, which are most popular to set up a virtual environment.
But we will be using the venv module which comes as a default with Python 3.x version and is recommended to use for virtual environment creation.
We will use Homebrew to install Python 3.8 and will then move on to creating a virtual environment. If you don't have Homebrew installed on your Macbook, you can install Homebrew on your MacOSX and then use it to install Python 3.8 on your machine.
Once you have Homebrew set up, run the following command to install the latest version of Python:
Homebrew will also install pip for you which you can verify by running the pip3 command.
To verify the successful installation of Python 3.x version, run the python3
command and the IDLE should start in your terminal.
venv
to create Virtual EnvironmentAs per the official documentation of venv
module,
The venv
module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
We can run the following command to create a virtual environment:
This will create a virtual environment for you with the following files in the virtual environment directory my_env:
To activate the virtual environment, run the following command:
This will start the virtual environment and you should see the name of the virtual environment added before the directory name as shown in the image below:
Now you can install anything in it, by running the pip3 install
command, for example to install the requests module, run the following command:
To get out of the virtual environment, run the exit
command.
With this you have the complete virtual environment setup, now create any .py script and try running it in the virtual environment. Also, you can install as many modules as you want in the virtual environment.
There is a known issue with version 3.7 of Anaconda Python and certain versions of the Mac OSX operating system. We strongly recommend that you use python 3.6 on OSX to avoid any problems. To install python 3.6, please follow the steps below:
To get started you need to download the OSX miniconda installer.
Click on the link https: https://repo.continuum.io/miniconda/Miniconda3-4.5.4-MacOSX-x86_64.sh to download the Python 3.6 installer.
Wait for it to download. It will place a file called Miniconda3-4.5.4-MacOSX-x86_64.sh in your downloads folder. On my computer using Safari this is ~/Downloads
Open a Terminal window. If you don’t know how to do this click * Applications -> Utilities -> Terminal*
Within the Terminal change directories into the folder where your downloaded file lives, i.e.
Now run the bash “shell” program to install Miniconda
Scroll through the license (press the space bar to move through quickly), type ‘yes’ to approve the terms, and then accept all the installation defaults.
Close the Terminal program. Then, restart it.
Within the Terminal type:
Enter y at the Proceed prompt.
When the installation completes, close the Terminal window.
The second half of the installation is the Spyder IDE development environment. This is the software that you use to write, edit and test your Python programs. Spyder IDE calls Python to actually run the program. Spyder is included by default in the Anaconda Python distribution, which comes with everything you need to get started in an all-in-one package.
However, with Miniconda, we have to download and install Spyder IDE.
Go to the Terminal window.
Type the following command:
This will take some time to download.
When the following prompt appears:
Type y, and hit enter.
Once the download is finished, type the following command to know the location of Spyder:
It is usually installed in the bin folder within miniconda3
To start Spyder, go to a terminal window and enter
You are running 64-bit Mac OS X.
Technically, all Apple computers purchased since Q4 2006 have been equipped with 64-bit capable processors. The Mac OS X operating system has supported 64-bit by default since OS X 10.6 Snow Leopard, released in 2009. The upgrade to the latest operating system for your computer is available for free, directly from Apple, since the release of OS X 10.9 Mavericks in 2013.