How to Use a Virtual Environment to run you Django app

Posted by Chris Bartos on March 1, 2016

The best way to use Python especially Django is by creating what is called a “Virtual Environment”. This is a separate instance of Python that you can install all the tools that you need to run your Django app without having to messing up your current Python instance.

It will also help to freeze the packages you install which is great for deploying to different environments and such. But, the steps are pretty much the same everything you create a new Virtual Environment. These steps are assuming you use a Unix based operating system. If you are using Windows, these steps will be a little different.

Step 1: Install ‘virtualenv’

First things first. You need to install virtualenv. You can do this with the ‘pip’ command.

$ pip install virtualenv

Step 2: Create your virtual environment

The next step to creating a virtual environment is to actually create it. This step will install an instance of Python and also install Pip for you so you can install the packages that you want.

$ virtualenv venv

You can call the virtual environment anything you like. I like to call it ‘venv’

Step 3: Activate the virtual environment

This step will change your Python path so that you will use the virtual environment version of Python and Pip. You’ll notice that when you enter this command, you might see ‘(venv)’ next to your commandline. That’s how you know that you’re running the virtual environment

$ source venv/bin/activate

Step 4: Install your packages

You can then install your packages at this point. You’ll be happy to know that you’re packages will be installed inside of your virtual environment. So, when you try to ‘import django’, the Python interpreter will be able to find it.

Step 5: Deactivate your virtual environment

When you are done working on your Django app, and you want to exit from your virtual environment, simply call:

$ deactivate

You’ll be back to your default Python path.

Similar Posts

What is SECRET_KEY in Django?
What is a Mixin


What do you do after the Official Django Tutorial?

Here's what you'll learn:

  1. Start writing tests
  2. Increase the size / complexity of the Polls application.
  3. Learn how to code in Django by reading the Django source code.
  4. Finding code snippets to use in your website by reading the code of other websites.
  5. Using Javascript in a web app.
  6. Deploying on Heroku and AWS.
We won't send you spam. Unsubscribe at any time. Powered by ConvertKit


Get some value from this post? Please like and share this post because more people also deserve some value. :-)