Setting Up Python Virtualenv

in programming •  last year


This is loosely a part 2 to my Getting Started with Bash on Windows post. If you already have a working Python development environment, you can skip that post and continue here.

What is a Virtual Environment?

Virtual environments are similar to virtual machines, but rather than emulating an entire system, virtual environments focus on a single application space for programs to run isolated from others.

Virtualenv will let you run multiple versions and instances of Python at once, without letting them interfere with eachother.

Setting Up Virtualenv

If you followed my Getting Started with Bash on Windows post, you're ready to open up your terminal and get started.

Getting virtualenv installed

First, we want to check and see if we already have virtualenv installed. Do this by typing:

virtualenv --version

If you didn't get a similar output to what's above, you probably don't have virtualenv installed. Install it using one of these commands:

sudo apt-get install python-virtualenv <--One I'd recommend for noobies

sudo easy_install virtualenv

sudo pip install virtualenv

Once it's installed, check to make sure by using the virtualenv --version command.

Prepare your virtual environment

Navigate to the directory where you want the virtual environment to be stored, and create a new folder to contain everything by using the mkdir command (or simply create a new folder in the file explorer). Name it something appropriate like "virtualenvironment_python_3.6".

Navigate inside this folder and create your virtual environment by running:

virtualenv my_app

If you want to isolate the site packages, add --no-site-packages to the end of this command.

After a short wait, you can now navigate inside your "my_app" directory to the "bin" folder and activate the virtual environment by using:

source activate

You will see that your shell prompt now says "(my_app)" at the beginning, indicating that you are inside your virtualenv.

Any packages you install using pip or easy_install will now be installed to the "my_app/lib/pythonX.X/site-packages" directory, completely isolated from other virtualenvs and your main system.

Virtualenv does not create a new Python environment, it uses links to existing files. This means you need an active Python environment already installed on your system.

Using your virtualenv

Lets run through a small example to demonstrate things.

To install a package to your virtualenv, use pip install X. No need to sudo the command since the files are being stored in the directory created by your user account. For example:

pip install flask

Now we can create a Python program using the Flask web framework.

To keep things in the terminal, lets run VIM and write a short program.


This should open VIM where you'll be greeted by the VIM welcome message.

i to inter insert mode.

If you remember the tips from my previous post, you can copy & paste what's below into your terminal next.

from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello World!'

if __name__ == '__main__':

Hit the esc key, and type:


This will (w)rite, (q)uit, and name the file "".

Now we can run our Flask program by typing:


It will inform you that the Flask app is running and on which address/port. You can now navigate to your web browser and enter this info to see your web app!

Hit Ctrl + C to shut down the Flask app when you're done.

Closing the virtualenv

When you're done using your virtualevn, simply use:


Now you know how to install and use virtualenv for your Python projects! I relied heavily on the following resources for writing this short tutorial, and if you encounter any issues they should be your first reference. If you can't find what you're looking for here, feel free to leave a comment below and somebody will try to help!

Python for Beginners - How to use Python virtualenv ••• Flask Quickstart guide

Leave a like or resteem if you found this helpful. You can also support me by sending ETH or ERC20 tokens to Tomshwom.eth. Leave your thoughts below about what you think and on which content you'd like to see in the future!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

This post received a 5.0% upvote from @randowhale thanks to @tomshwom! For more information, click here!

virtual environments focus on a single application space for programs to run isolated from others

You make it sound like it's a container solution that provides process isolation. Doesn't it just provide you with an environment with all the dependencies you need to run your program?


That is correct. It's not process isolation, just package/environment isolation.


Thanks for clearing that up. I wasn't sure if I had missed some kind of update that added container support.

another option is installing Anaconda. Below video explains the details.