Skip to content

Instantly share code, notes, and snippets.

@halfak
Last active January 17, 2023 22:50
Show Gist options
  • Save halfak/9f4830895496af9e9731 to your computer and use it in GitHub Desktop.
Save halfak/9f4830895496af9e9731 to your computer and use it in GitHub Desktop.
Setting up a python 3.x Virtual Environment

Step 0: Set up python virtualenv

virtualenv is a command-line utiltity that will allow you to encapsulate a python environment. Ubuntu calls the package that installs this utility "python-virtualenv". You can install it with $ sudo apt-get install python-virtualenv.

Step 1: Create the virtualenv directory

In this sequence, I'm going to assume that python 3.5 is the installed verison.

$ cd ~
$ mkdir venv
$ cd venv
$ virtualenv -p $(which python3) 3.5 --system-site-packages
$ cd ~

Step 2: Activate the virtualenv

You'll need to 'activate' the virtualenv in order to make use of it. It will re-write your prompt to put the name of the virtualenv at the beginning.

$ source ~/venv/3.5/bin/activate
(3.5)$ 

I recommend that you update pip immediately because there are sometimes some issues in old versions of pip that get in our way:

(3.5)$ pip install pip --upgrade
Collecting pip
.........
Successfully installed pip-19.3.1

Step 3: Do python 3 things

(3.5)$ python --version
Python 3.5.3
(3.5)$ pip --version
pip 19.3.1 from /home/halfak/env/3.5/lib/python3.5/site-packages (python 3.5)

Step 4: Deactivate the virtualenv

When you want to go back to the default python state, you'll need to deactivate the environment.

(3.5)$ deactivate
$ python --version
Python 2.7.6
$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

Any time you want to use the virtual environment again, just run that "source activate" command from above:

$ source ~/venv/3.5/bin/activate
(3.5)$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment