Quick Start to Virtual Environments in Python

If you are installing everything globally you are also updating everything globally.

If you created a project months ago and recently needed to update your global copy of a library — like pandas — and this update can break your project.

This kind of problem is called a library version conflict.

Rule of ThumbThis doesn’t mean everything you use needs to be installed locally.

If there are packages or libraries that you are using outside of projects you can install these globally.

Anything that is a dependency should be installed locally in your virtual environment.

This may become more important as you create projects that are no longer on your device.

Tutorial: Setting up the Virtual EnvironmentFirst, we need to install virtualenv as shown below:Before this step, you can check to make sure that it is not already installed on your system by running virtualenve — version in the terminal.


Now that we have successfully installed it on our machine, we should create a folder to store all of our projects and environments.

Navigate to where you want to create this folder as shown below, mine will be on the desktop:3.

Navigate to this new directory, and create your virtual environment using: ‘virtualenv your_project_name’after you do this you should get a message: “installing setuptools, pip, wheel….


” This is what you want.

To use a different version of python than the base prefix one, you can do the following:4.

Now that we have created our new virtual environment, we need to activate it to be able to work inside of it.

Use the first line from the snippet below to activate:In the screenshot below, I activated the environment django:As you can see by the prefix “(django_1)”, I am now working in the environment I named django_1.

After, we can look to see which python the environment is using and can even check the version.


At this point, we are working in a virtual environment that only has pip, setuptools, and wheels available to it.

If you look back to where we created this environment those were automatically “installed”.

Let's install some useful libraries and check it out:Above you can see that I installed Numpy, Pytz, and Psutil.

We can use “pip list” to show all of these are available to our virtual environment:Great our libraries are all there!6.

Now, if we want to work on a different project or we are done with this one all we have to use it the keyword “deactivate”:Here you can see that the prefix of our environment name has disappeared in the second line to show that we are now out of our environment.

You can easily activate your environment again by using the line from before “source your_enve_name/bin/activate”.

Note: you should not be saving your project files into this virtual environment.

All that goes into these environments are packages and dependencies — especially the second one.


. More details

Leave a Reply