Django Djourney: Setting up a Python virtual environment

Jessica Salbert
4 min readFeb 8, 2021

I’ve found myself with a bit of extra time in 2021, so I’ve decided to dive deeper into my first (and perhaps most beloved) language — Python! I need to spin up a relatively simple backend for a personal project, and figure I’d take the opportunity to learn a new framework. So here begins the start of my Django exploration.

*Note that the steps & commands I’ve included are for MacOS

Before we even think about models or routes, this post will walk you through (and serve as a reminder to myself of) the steps I took to set up my local environment to use Python (and later Django). An important note is that it’s recommended to manage each Python program within its own virtual environment. This is a sandbox where dependencies can be saved independently of other programs. It allows us to build multiple projects using different versions of the same package.

1. Download Python & pip

If you haven’t already, take a minute to install Python. We’ll also need to become familiar with pip, which is Python’s package manager. Think npm in Javascript or gem in Ruby — it allows us to manage and install third party libraries and dependencies. Later Python versions actually come installed with pip, so go ahead and run pip — version to check if you have it.

Installation instructions for pip are here if you’re running an older version of Python. You can check your current Python version by running python -V in the terminal — for this tutorial I am using Python3.

2. Download pipenv

Now that we have Python and pip installed, we are ready to set up a virtual environment to build a project in. I used the pipenv module. A few notes on how pipenv is different from other virtual environment modules:

  • It creates a Pipfile which keeps track of all the dependencies that are installed in that particular virtual environment.
  • It stores all of the environment-related files (bin, lib, include, etc) in a separate location on your local machine. This is nice — it prevents local virtual environment files from being pushed to github or production. Your project folder will hold only files needed for your project.
  • You can install dependencies and run commands that apply to the virtual environment, even when it isn’t activated.

First cd into your project directory (or create it). Install pipenv by running python3 -m pip install pipenv. It might be a good idea to add pip -- upgrade to ensure that your package manager is upgraded. You can also install pipenv via Homebrew.

If installed correctly, you should run pipenv and see the following output, summarizing the commands available to pipenv.

Great, you’ve got pipenv installed! We’ll now use it to install additional dependencies directly into this virtual environment. pipenv install is the command we’ll use to do that moving forward.

We’ll first install the latest version of Python into the virtual environment using the command pipenv install — python python3. The ‘python3’ part specifies the version you want to install — otherwise it will default to the Python version that is globally installed on your Mac, which might not be the version you want. Again, you can check this by running python -V outside of your virtual environment.

Ensure that the version of Python specified in the output below is the one you want for this project. You can also check Python version noted in your Pipfile.

Another note- the “Virtualenv location” path is shown in green. This is where all of your virtual environment-related files are stored, separated from your actual project files.

3. Activate and deactivate virtual environment

The command for activating your virtual environment with pipenv is pipenv shell.

When your virtual environment is activated, you’ll see the project name in parentheses to the left of your current path in the terminal (shown above). If you’re ever unsure of if your environment is activated, just run pipenv shell and it will let you know if it’s already been activated.

Deactivating the environment couldn’t be simpler. Just run deactivate.

If you try to activate the virtual environment after already having activated and deactivated it, you may get a Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated message:

No worries — simply restart your terminal and activate again. You can copy your current path using pwd, then use cd and paste the the path in a new terminal window.

So there you have it — a shiny new virtual environment ready for the Python project of your choosing. I’m going to move forward with a Django project, which I’ll be setting up next week. Stay tuned!

The following video does an awesome job of laying out the above steps and getting started with Django — I’d recommend for visual learners!

--

--