Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: Creating a virtual environment
0:00 Let's see virtual environments in action.
0:02 So, first we need to create one.
0:05 Python has a built in module called venv to manage virtual environments,
0:09 so we don't even have to install anything.
0:11 We create a new virtual environment with a command python -m venv.
0:16 And then we specify the name of the virtual environment.
0:20 The -m parameter tells Python to call the built in module venv and module venv
0:26 takes one parameter the name of the visual environment.
0:30 This created a folder called my_first_virtualenv.
0:33 in the current directory. my_first_virtualenv is a silly name for a
0:37 virtual environment. But I just wanted to show you that you can name it however
0:41 you want. A much more common convention is to name this folder venv
0:45 or .venv. This makes it obvious that whatever is inside this folder is related
0:51 to a virtual environment. But it also has another benefit.
0:54 Some python editors, like PyCharm or VSCode,
0:57 will automatically recognize this folder as a virtual environment,
1:02 and we'll start using it in your project.
1:05 Okay, so we created a virtual environment.
1:07 But how do we use it, inside our folder.
1:10 There is a bin directory, and there we have activate script.
1:13 Since it's a bar script, we have to source it with the following command
1:16 source activate, if you are not using bash but,
1:20 for example, Fish shell, you have to source the appropriate file in this case,
1:24 the activate.fish. After the activation,
1:27 you can see that my prompt has changed.
1:29 And even though I'm using a custom prompt if you're using,
1:32 let's say bash or just plain Z shell,
1:35 you should also see the name of the virtual environment displayed.
1:39 Let me show you. Let's say I'm using Bash and you can see it's here
1:44 So it's easy for you to tell if your insight of the virtual environment or
1:49 not, let's go back. And if for some reason you don't see the name
1:54 of the virtual environment in the prompt,
1:56 you can quickly check that you are inside the virtual environment by running.
1:59 pip -V. If it's pointing to a pip binary inside my first virtual evironment folder then it means that you are using virtual environment.