Manage Dependencies with Python Virtual Environments

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

Virtual Environments ensure that dependencies from one Python application don’t overwrite the dependencies of another application. In this lesson, you will learn how to create a virtual environment, switch between virtual environments, and manage dependencies within a virtual environment.

Virtual environments allow us to keep dependencies for different projects in separate places. To use it, we need to install it, so we'll type pip install virtual env, and I've already got it installed, so there's nothing for it to do.

To get started using it, I'll create a directory for my project, change into that directory, and then create the virtual environment with the virtual env command, and a name for my virtual environment.

At the same time, I can also tell it which Python I want to use, whether it's Python 2 or Python 3. Since my system defaults to Python 2, I want to use Python 3, so I'll provide the path to Python 3.

If we take a look at the directory here, there's a new directory called py3, and if we look inside that directory, there's all of the Python requirements to run that virtual environment.

Look at this. I can type python--version, and it shows I'm running Python 2.7. To activate this virtual environment, I'll type source, the name we gave it, the bin directory, and the activate command.

Notice that it changed my prompt there to show that I'm now working from the py3 virtual environment. If I run the same command, python--version, I'm now running Python 3.6.1.

I can use Python like I normally would, including installing packages using pip. I can pip install Request to install Request Library. That gets installed, and now watch this.

I can type pip freeze, and pipe the output of that to a file called requirements.txt. If we take a look at that, it's listed all of the libraries that I have installed in this virtual environment, as well as their version.

What that means is I can include this requirements.txt as part of my repo for this. Whenever someone else checks out this repo, they can type pip install-r, requirements.txt, and pip will install the exact same requirements that this virtual environment specifies so that we know that everyone's running on the same dependency versions.

When I'm all done, I can just type deactivate. Notice that my cursor changed back to remove me from the virtual environment, and I can type python--version, and I'm back on Python 2.7.

One thing you'll want to do, though, is be sure that you exclude this py3, or whatever name you gave your virtual environment, from your source control system so that the virtual environment itself isn't included in your repository chest or requirements text specifying the library versions.

Raunaq Sahni
Raunaq Sahni
~ 4 years ago

Hello Will,

I'm a little confused about why we install virtualenv using pip and not pip3.

Will using the latter cause unwanted behaviour while using virtualenvs for different python projects?

Also, would you recommend using the built-in venv instead of virtualenv if one does not have to support Python[2]?

Thank you.

Will Button
Will Buttoninstructor
~ 4 years ago

That's a great question Raunaq! It's done that way to ensure the command works, not matter which version of python you start with. pip is a symlink to whichever python environment is active.

As an example, suppose you are working with a new server with python2 installed as the default. Using pip3 would try to install a virtual environment using pip3, which may not work. Using the pip symlink ensures virtualenv is installed correctly for the active version. Once you activate the new virtual environment, the pip symlink is updated to point to pip3, and all packages installed using it will install the python3 libraries.

If you don't have to support python2, venv is a great way to go. Additionally, if you're deploying a single app to a single purpose server (like a Docker container that will only run your app), there is no need for a virtual environment at all. Install python3 as the default and avoid the hassle! :)

Raunaq Sahni
Raunaq Sahni
~ 4 years ago

Hello again Will,

Thanks a lot for the quick and thorough response. Thanks for the extra info regarding deployment to a single purpose server.

Best,

Raunaq

Victor Hazbun
Victor Hazbun
~ 4 years ago

how do I install pip on Mac OS?

Victor Hazbun
Victor Hazbun
~ 4 years ago

how do I install pip on Mac OS? sudo python -m ensurepip

Will Button
Will Buttoninstructor
~ 4 years ago

Hey Victor, pip has been included with Python since 2.7.9 and 3.4 so it should already be there. Try which python to see where python is installed. You should find pip installed in the same directory.

VARUN
VARUN
~ 4 years ago

source py3/script/activate is not actaviting my virtualenv please help

Will Button
Will Buttoninstructor
~ 4 years ago

Hey Varun, What command did you use to create the virtualenv?

VARUN
VARUN
~ 4 years ago

Hello Sir, I am on windows 10 operating system and i have followed your instruction on video with the same command you typed in. Thank You

Will Button
Will Buttoninstructor
~ 4 years ago

Ah, I don't believe source works for Windows, it's a bash alias available in Linux/OS X. Take a look in the py3\ folder. There should be a Scripts folder with an activate script in it. So you would activate your virtual environment by running py3\Scripts\activate

Niklas
Niklas
~ 4 years ago

Not sure why I am getting

pip install virtualenv
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal import main```
Will Button
Will Buttoninstructor
~ 4 years ago

Hi Niklas, There can be many reasons for this. What operating system are you using? What version of pip?

This provides some common solutions: https://github.com/pypa/pip/issues/5253

Sunil
Sunil
~ 4 years ago

Dear Will,

I see now the official way to Virtual Env is the Pipenv, Could you please update the lesson on this?

BrightPixels
BrightPixels
~ 3 years ago

Dear Will,

I see now the official way to Virtual Env is the Pipenv, Could you please update the lesson on this?

Ron
Ron
~ 3 years ago

On mac, pip is not installed by default. After I install python3, pip3 will be installed, and just pip3 install virtualenv, and all the following commands are all working without problem.

Not sure if it is a good approach.

Will Button
Will Buttoninstructor
~ 3 years ago

Egads! Sorry for the oversight Ron! That seems like a great approach.

josh
josh
~ 3 years ago

Does python / pip not have the concept of a lockfile? If not, that's a huge weak spot in the python ecosystem. If yes, it would have been prudent to include that in this course.

Will Button
Will Buttoninstructor
~ 3 years ago

It does: pip freeze > requirements.txt will store the installed dependencies in the file requirements.txt. Use pip install -r requirements.txt to install them in a new environment (such as a deployed server)