Experienced Technology Entrepreneur
Chief Encouragement Officer

Experienced Technology Entrepreneur
Chief Encouragement Officer

How to Create a Python Virtual Environment MacOS

39867

Posted by Paul Helmick

I’m a Technology CEO and Experienced Entrepreneur. I love helping people use technology to grow their business. 

  • This is a quick summary of the MacOS bash commands to create a Python3 Virtual Environment
  • The Default MacOS kernel runs Python 2.7
  • The best practice is to NOT install packages for development / code testing into your core OS stack
  • The best practice IS TO install Python3 and then use Virtual Environments for specific coding projects
  • Benefit: This allows each specific coding project to run from a clean baseline Python3 install and only load the specific packages and dependencies needed in the requirements.txt file

Source Article on Real Python

python virtual environment

Code Snippets

# check versions
python --version
pip3 --version

# update python package manager
pip3 install --upgrade pip3 

# install virtual env package
pip3 install --user pipenv

# This does a user installation to prevent breaking any system-wide packages. If pipenv isn’t available in your shell after installation, you’ll need to add the user base’s binary directory to your PATH.

# find pipenv bin path
python3 -m site --user-base

# edit profile path defaults
cd ~
nano .bash_profile

# add path to profile
PATH="~/Library/Python/3.7/bin:${PATH}"
export PATH

# exit nano and reload profile
source .bash_profile

virtualenv --version

# create virtual env
cd project_folder
python3 -m venv env

# activate venv for use
source env/bin/activate

# note the bash prompt shows (venv)

# install packages into venv
pip3 install requirements.txt 
pip3 list

# exit the venv
deactivate

Related Articles: