How to Install Virtual Environment in Python on MacOS?

In this article, we will learn how to install Virtual Environment in Python on macOS. The virtualenv is a tool to create isolated Python environments. Since Python 3.3, a subset of it has been integrated into the standard library under the venv module. The venv module does not offer all features of this library, to name just a few more prominent:

  • is slower (by not having the app-data seed method),
  • is not as extendable,
  • cannot create virtual environments for arbitrarily installed python versions (and automatically discover these),
  • is not upgrade-able via pip,
  • does not have a rich programmatic API (describe virtual environments without creating them).

Installation:

Method 1: Using pip to install virtual environment Package

Follow the below steps to install the virtual environment package on macOS using pip:

Step 2: Check if pip3 and python3 are correctly installed.

python3 --version
pip3 --version

Step 3: Upgrade your pip to avoid errors during installation.

pip3 install --upgrade pip

Step 4: Enter the following command to install virtual environment using pip3.

pip3 install virtualenv

Method 2: Using setup.py to install virtual environment

Follow the below steps to install the virtual environment package on macOS using the setup.py file:

Step 1: Download the latest source package of virtual environment for python3 from here.

curl https://files.pythonhosted.org/packages/dd/40/9bc1b32521f78c293c1f8ca423c725737dfa9d09640dbeec61cebca7c5f2/virtualenv-20.8.1.tar.gz > virtualenv.tar.gz

Step 2: Extract the downloaded package using the following command.

tar -xzvf virtualenv.tar.gz

Step 3: Go inside the folder and Enter the following command to install the package.

Note: You must have developer tools for XCode MacOS installed in your system

cd virtualenv-20.8.1
python3 setup.py install

Verifying virtual environment installation on macOS:

Make the following import in your python terminal to verify if the installation has been done properly:

import virtualenv

If there is any error while importing the module then is not installed properly.


Contact Us