Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating and Activating a Virtual Environment

00:00 I am going to jump into a terminal session now, to show you how you can create and activate these virtual environments. Alright, I’m in my terminal here and now I am going to show you how to create your first Python virtual environment.

00:13 So, the first thing I want to demonstrate to you is when I use the which command to look up where the pip executable is right now. You can see here it’s inside /usr/local/bin/pip3, which is the global shared environment.

00:28 Now if I would go ahead and run pip install and install some library, it would end up, well, not exactly here in this folder, in the bin folder, but it would end up in the global environment.

00:39 So, the way around that is by creating a virtual Python environment. Now let’s assume we wanted to start a new Python project, so probably create its own folder for that, so I would create a directory let’s call that test-project.

00:56 Switch into that test project, and you can see here that right now, this is completely empty. So what I am going to do now is I am going to create a virtual environment, with this command here, so you want to go python 3 -m venv if you are on Python 3, on Python 2 it’s a little bit different but I am going to walk you through that later.

01:19 And then as the last argument here, you can pass the name of the folder where you want to store that virtual environment, or where you want that virtual environment to be created.

01:34 Now, personally, I use a very simple naming scheme, I just create my virtual environments right inside the project folder, and I usually call them venv.

01:43 So, personally, I would do something like this, but of course, you could also have a shared folder, like a Python environments folder, where all of your Python environments live and then you are going to be able to reuse them across different projects.

01:57 Now, personally, I don’t recommend that, so this is what I like to do. Okay, so this just took a second here to set up the virtual environment, and now, when I check what is inside this folder, we can see here that now we have this venv folder.

02:12 And when I check what is inside the venv folder, you can see here that there is a bunch of files that are part of this new Python environment.

02:21 Now why don’t we take a closer look at this venv folder? So you can see here that there is a lot of stuff inside that folder, because this is actually a completely isolated and separate Python environment.

02:32 Now this is not going to be very interesting, because, it’s just the Python internals here, but this should give you an idea that a Python virtual environment is actually a completely separate Python environment, and that is exactly what we want.

02:45 Alright, so we created a virtual environment, and if I were to run this pip3 command, or the pip command now, it would actually still point to the global environment, so there is one more step we need to take here.

02:58 And that is we need to execute a script inside the virtual environment. And, it’s this one here, so inside the virtual environment, you want to go into the bin folder and look for the activate script.

03:09 And so when I run this, this activates the virtual environment, and you can see that here that running the script out of this little marker here to my shell prompt, now it tells me that I am inside, or that I have activated this virtual environment called venv.

03:24 So that is just a folder name that I used earlier. Now, when I use this which pip command again, you can see that now this is actually pointing to a different location, so now this points to the separated and isolated environment that I just created.

03:41 And the same thing is true for the Python interpreter, so now if I were to run the Python interpreter, it would actually load it from inside the virtual environment and not from my global environment, which is exactly what we want.

03:54 So this is how you create and activate the Python virtual environment, so here is a quick recap on what I just showed you, so on Python 3.3 and above, it’s really easy to manage your virtual environments because the venv command, or the venv module that manages them is actually part of the Python distribution, so you can just use that python -m venv and then the name of the folder where you want to create that virtual environment.

04:21 But on older Python versions, it’s a little bit different, so for those versions of Python, you typically need to install the virtualenv package manually, and then you would use the virtualenv command and it would kind of follow the same syntax to actually create a virtual environment, and you would activate it in exactly the same way.

04:41 So that is a little difference you need to be aware of, maybe one extra step you need to take before you can start creating your virtual environments. On Windows, the step you need to take to actually activate a virtual environment is slightly different, so we’re not using the source command there, to load the activate script, but instead, we’re just running the activate command or activate script from the scripts folder, so that is a small difference, but in all other aspects, it’s very similar to how it works on Linux and macOS.

Avatar image for 13carlestecno

13carlestecno on Sept. 28, 2019

Hi, Good videos. I would like to know ” wich pip3” on windows.

Is it pip –version?

Thanks

Avatar image for Dan Bader

Dan Bader RP Team on Sept. 28, 2019

On Windows you can use the where.exe command (see this StackOverflow thread for other options). This should work on recent versions of Windows:

C:\> where pip
Avatar image for 13carlestecno

13carlestecno on Sept. 29, 2019

Thanks, Dan.

Avatar image for mutley75

mutley75 on March 29, 2020

Hi Dan,

Your course is well laid out and a has some neat tricks I had no idea I could use. Do you have a course that shows setting up these Virtual Environments in VS Code?

Avatar image for Jiri Jahn

Jiri Jahn on June 13, 2020

Hi Dan,

I am a bit perplexed: I am on Ubuntu 20.04 where the system seems to come with native Python 3.8.2, without pip. After I installed pip with sudo apt-get install python3-pip I cannot see the venv package after I enter pip3 list and also the command python3 -m venv ./venv (after your hint) outputs

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv

I am going to install the venv package manually, hoping not to cause too much harm. Or is it better to reinstall Python anew?

Avatar image for umbertomorelli1988

umbertomorelli1988 on July 5, 2020

When issuing python3 -m venv ./venv on Windows 10 I received:

Error: [WinError 2] The system cannot find the file specified

I solved by upgrading Python (3.7.x to 3.8 - using the installer, although I had to manually remove the original folder) and issuing the following command in Powershell:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

As suggested at https://docs.python.org/3/library/venv.html.

Avatar image for johnelutz

johnelutz on July 25, 2020

Hi! In this video, you have mention a line of code in two different places, with a slight difference of syntax; should it be: python3 -m venv ./venv or python3 -m venv venv ? Thanks!

Avatar image for Dan Bader

Dan Bader RP Team on July 26, 2020

In this case

$ python3 -m venv venv

and

$ python3 -m venv ./venv

both have the same result. The ./ (dot slash) at the start indicates a relative path to the current directory, more here:

unix.stackexchange.com/questions/397524/what-does-the-mean-dot-slash-in-linux

Avatar image for Paulo Szewierenko

Paulo Szewierenko on Aug. 8, 2020

Hi Dan.

I’m confused. I manage python packages through Ananconda prompt. In conda managing environments tutorial I found this:

We recommend that you:

Use pip only after conda

  • Install as many requirements as possible with conda then use pip.
  • Pip should be run with –upgrade-strategy only-if-needed (the default).
  • Do not use pip with the –user argument, avoid all users installs.

Use conda environments for isolation

  • Create a conda environment to isolate any changes pip makes.
  • Environments take up little space thanks to hard links.
  • Care should be taken to avoid running pip in the root environment.

I think this conflict with some concepts in your course. Am I right?

Thanks!

Avatar image for DoubleA

DoubleA on Jan. 16, 2021

Activating a new venv in cmd:

Use the activate script in the Scripts directory of your virtual environment:

venv\Scripts\activate

This will activate your virtual environment and your terminal will look like this depending on the directory you’re in:

(venv) C:\Users\acer\Desktop>

Hope this helps to someone as dumb/clever as myself :)

The source

Avatar image for jnario

jnario on Jan. 22, 2021

It may be worth mentioning that by adding a --prompt switch to the end of the venv command, you can specify the name that will appear in your prompt when you activate the environment.

So,

$ python3 -m venv ./venv --prompt myproj

Would result in a (myproj) prefix on the prompt, instead of the name of the ./venv folder. This can be handy if you have a few prompts and projects open at the same time (or just want a bit more context in the prefix).

Avatar image for Dan Bader

Dan Bader RP Team on Jan. 22, 2021

Great, thanks for sharing @jnario!

Avatar image for tslilon

tslilon on Jan. 28, 2021

Hi! Thank you for this tutorial.

I was wondering whether having the entire venv inside a project directory is a good practice when collaborating with others with git. Would appreciate your thoughts on this!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 28, 2021

@tslilon That’s a legitimate concern! As long as you add the entire venv/ subfolder to .gitignore, I don’t see why it would hurt. Personally, I prefer to keep my projects separate from the virtual environments, which I store in one central place. That way, I can quickly find all of my virtual environments and switch between them using virtualenvwrapper in the command line.

Avatar image for sundevilthor

sundevilthor on July 5, 2021

What is the -m for in python3 -m … ? And where can I find a list of items to substitute there? I know that -i will open up an interactive shell after running the program, but that is the only one I am aware of.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on July 6, 2021

@sundevilthor When you type python --help in the terminal, it will list the available options and arguments:

$ python --help
usage: /home/jdoe/.pyenv/versions/3.9.5/bin/python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : turn on parser debugging output (for experts only, only works on
         debug builds); also PYTHONDEBUG=x
(...)

The -m flag lets you execute a Python module as a script. It can be a module either from the standard library or one of your own. For instance, you can start an HTTP server using the built-in module:

$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Become a Member to join the conversation.