Setting Up Python Virtual Environment (on Mac)

Working with a Terminal (Command Line) makes me feel cool. Like a hacker. Or a proper IT gal. Like someone who knows what they’re doing. 🙂

When I find new toys to play with, I prepare a proper playground. It’s called a virtual environment and it’s pretty useful. But why? What is it good for?

As they say on GeeksForGeeksA virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.

To be honest, the best way around a command line is googling. Nevertheless, those are my step-by-step notes for junior developers as I am. Where to download Python, how to create a new directory (folder), what are the most basic commands in a command line and how to set up a virtual environment.

Install Python

Step one, make sure you do have your Python installed (whichever version you prefer). No idea whether it’s there? Ask your Terminal! It knows everything. Simply open a Terminal and write:

python --version
python3 --version

If not, go download it and install it. When using Python on Mac, writing python usually gets you Python 2.X, when you want to use Python version 3, simply use command python3 as seen above (or below).

OR! Install Python in a command line:

# install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# brew preferred version of python
brew install python3

To learn more about brewing, check this out.

Download and install pip as well if needed (it should be there but shit happens). Pip is a tool for Python packages installation.

# download pip
pip curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

# install pip
python3 get-pyp.py

Creating new directory & basic Terminal commands

Once Python lives in your terrarium called a Mac, open a Terminal (if you haven’t already) and create a directory (mkdir = make directory) to keep your working space nice and clean and navigate yourself there (cd = change directory). mkdir simply makes a folder right where you are – probably in your home folder – do you want to create it somewhere else? Navigate yourself there with cd command, ie. cd Desktop/someFolder/anotherFolder or add this path when creating the folder.

mkdir awesomeProject
mkdir Desktop/someFolder/awesomeProject

cd awesomeProject
cd Desktop/someFolder/awesomeProject
# go to home folder
cd

# go one level back
cd ..

# go to root level
cd /

# rename or move stuff
mv code.txt code.py
mv Desktop/code.txt Desktop/someFolder/awesomeProject/code.txt

# list everything in a current directory
ls

Can’t wrap your head around it? Check this article.

Virtual environment

Let’s create a virtual environment called env (just to keep things simple and because it’s done this way):

python3 -m venv env

# this would install a virtual environment with Python 2.X
python -m venv env

Activate! How do you know your virtual environment is activated? At the very beginning of your command line, you’ll see (env) – name of your virtual environment in round brackets. Just so you know, from now on and only here in the virtual environment, your Python 3 will use an alias: python.

source env/bin/activate

Install all the libraries and modules you need (here’s just an example):

# install numpy with pip
pip install numpy

When you need to share your virtual environment with someone or just need to save the current state of installing requirements, here’s what you need to do:

# list what's installed in your virtual environment 
pip freeze

# save those requirements
pip freeze > requirements.txt

Also, keep in mind your virtual environment stays active until you close the Terminal window or execute this simple command:

deactivate

One more thing, when you need to kill any execution, Ctrl+C is what you’re looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *