Python Environments

Page Contents

Virtual Environments


  1. A non-magical introduction to Pip and Virtualenv for Python beginners by Jamie Matthews.
  2. Pipenv & Virtual Environments, Hitchhier's Guide to Python.
  3. Python Virtual Environments: A Primer
  4. Pipenv: Python Dev Workflow for Humans

Note: Recommend PipEnv - superceeds virtualenv - see next secion!


# Install
pip install virtualenv

# Create project virtual env for Python 2
cd my_project_folder
virtualenv my_project # virtualenv my_project will create a folder in the current
                      # directory which will contain everything you need for this
                      # private environment

# Create for Python 3
python3 -m venv env

# Use the venv
source my_project/bin/activate
# ... Your prompt should become prefixed with your environment name
# ... After activating, a new path to the Python interpretter in your
# ... environment folder takes precedence so it will be used. Same for
# ... the python package path lookup etc...

# Install packages as usual
pip install <package-name>

# When finished...

# Delete a venv
rm -fr my_project

# Snapshot of current state:
pip freeze > requirements.txt

# Load snapshot
pip install -r requirements.txt


On GitHub.

You can supplement VirtualEnv with something called AutoEnv. It is a little utility that will monitor your change-directory commands. When you cd into a directory that contains a .env file it will try to source that file. So, if inside that file you have the line source my_project/bin/activate, when you change into that directory, it will automatically activate your project for you. Happy days!


The author of AutoEnv actually recommends trying this above AutoEnv. TODO.

Pipenv: Python Dev Workflow for Humans

See Pipenv: A Guide to the New Python Packaging Tool.

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world ...

... It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages ...

... The problems that Pipenv seeks to solve are multi-faceted:

  • You no longer need to use pip and virtualenv separately. They work together.
  • Managing a requirements.txt file can be problematic, so Pipenv uses Pipfile and Pipfile.lock to separate abstract dependency declarations from the last tested combination.
  • ...

... Pipfiles contain information for the dependencies of the project, and supersedes the requirements.txt file used in most Python projects.

Install using pip3 install pipenv and create a new virtualenv using pipenv --python 3[.minor].

Now, to add packages to your project use pipenv install <package>[~=maj.min[.patch]]

Like the original environments, it needs tobe actived: pipenv shell. Creates virtual environment if one doesn't exist. Note, pipenv creates all virtual environments in a default location.

To push to production, lock your environment: pipenv lock. Creates/updates the Pipfile.lock file (never edit this!). To install from your lock file use pipenv install --ignore-pipfile.

To leave a pipenv virtual environment just exit the current shell: pipenv starts a new shell session with the virtualenv pathing instead of changing the pathing in the current shell session.