Manage Dependencies with Python Virtual Environments

InstructorWill Button

Share this video with your friends

Send Tweet

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.

Raunaq Sahni
~ 6 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 Buttoninstructor
~ 6 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
~ 6 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
~ 6 years ago

how do I install pip on Mac OS?

Victor Hazbun
~ 6 years ago

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

Will Buttoninstructor
~ 6 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
~ 6 years ago

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

Will Buttoninstructor
~ 6 years ago

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

VARUN
~ 6 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 Buttoninstructor
~ 6 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
~ 6 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 Buttoninstructor
~ 6 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
~ 6 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
~ 5 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
~ 5 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 Buttoninstructor
~ 5 years ago

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

josh
~ 5 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 Buttoninstructor
~ 5 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)