Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: Virtualenvwrapper: a virtual environments management tool

Login or purchase this course to watch this video and the rest of the course contents.
0:00 venv module is perfectly fine for managing virtual environment,
0:03 but I want to show you another tool that I have been using for a long
0:06 time. It's called virtualenvwrapper,
0:09 and it comes with a lot of cool features that makes working with virtual environments much
0:13 easier. virtualenvwrapper is available on Linux and Mac OS.
0:17 If you're on Windows, check out the virtualenvwrapper-win or
0:23 virtualenvwrapper-powershell. They're both parts of virtualenvwrapper for Windows.
0:27 One will work with the standard terminal and the other one will work with Power Shell
0:31 However, be aware that virtualenvwrapper won't work.
0:36 If you are using pyenv, let me show you why.
0:39 So, Only follow the next steps.
0:41 If you are not using pyenv and for all those pyenv users watching
0:46 this, I will show you what you can use instead in a moment.
0:49 So, if you're not using pyenv and you want to install virtualenvwrapper,
0:53 we can do this, for example,
0:54 with pip. Let's copy this.
1:04 Okay, next we have to
1:05 also, add some environment variables,
1:07 to our shell, no matter if you're using bash or zshell scroll down a bit
1:12 and then copy those three lines and put it either in your zshell rc or bash rc
1:18 Since I'm using Zshell,
1:21 I'm gonna edit zshell. I just put a note that this for virtualenvwrapper
1:28 Okay, Now we can actually restart zshell and it should work,
1:34 except that it's not working. So I told you,
1:36 this is not going to work for pyenv because actually,
1:39 the virtualenvwrapper was installed in a different directory.
1:42 We can check it using the which Command.
1:44 As you can see, virtualenvwrapper was installed inside our pyenv directory.
1:50 And even if we use this one,
2:01 if we try to source it,
2:02 it's actually going to fail. Okay,
2:05 first time it works, and the next time it's actually going to crash our shell
2:10 Yeah, we can. If you do this,
2:13 you will no longer be able to start shell.
2:15 So, don't do this. That's why I told you to not follow those steps.
2:18 If you're using pyenv. So if you are not using pyenv virtualenvwrapper
2:23 should be installed for you And if you are using pyenv,
2:25 let me show you what you can use instead.
2:27 So, virtualenvwrapper is not going to work for you.
2:30 But there is a plugin for pyenv called.
2:33 Well, pyenv-virtualenvwrapper.
2:36 When you install it, you will get the same set of commands that virtualenvwrapper
2:39 provides. So let's do this now.
2:43 Installing pyenv plugins is easy.
2:45 You just have to clone the GitHub repository inside your pyenv/plugins
2:49 folder. Once this is done,
2:53 let's scroll a bit down to see the usage.
2:56 So, we have to run pyenv virtualenvwrapper command to initialize this plugin
3:00 If it's not working, just restart your shell,
3:06 and this time it worked. So now you have access to all the commands that
3:10 virtualenvwrapper provides. And speaking of those additional commands,
3:14 let's see what you can actually do now. To create a new virtual environment.
3:18 You just have to call mkvirtualenv command and the name of the virtual
3:22 environment. And as you can see,
3:24 it's automatically activated. You can create more virtual environments.
3:28 You can deactivate it. To list
3:30 All the environments you can run lsvirtualenv.
3:34 You can see, we have two virtual environments and to remove them,
3:37 just run rmvirtualenv, and that will actually delete both of them.
3:41 The difference between venv module that comes with Python and the virtualenvwrapper is that
3:47 for virtualenvwrapper, it doesn't matter in which folder you're on your commands.
3:52 It will create all your virtual environments in a special directory in your home folder called
3:57 virtualenv. So, we can go there and here we don't have virtualenv yet.
4:07 You can see we just created a new virtual environment and that created the folder here
4:12 so your virtual environments are no longer stored together with your projects,
4:16 but they're stored all in one folder.
4:20 Let's go back to our example with two Django applications.
4:24 And as you can see, I'm on Django 2 virtual environment, so I can go
4:27 to client_django_app, and I can actually keep install some stuff and start
4:33 working on it. If I want to switch to this other Django app,
4:40 I don't have to go to a specific folder to create a new virtualenv.
4:44 I can on it, for example,
4:45 in this work directory and as you can see,
4:49 I automatically switched django 3.If I goto this other Django app,
4:54 I don't have this .venv folder here.
4:57 All the virtual environments that I created with virtualenvwrapper are stored in my home
5:03 directory in this .virtualenv folder.
5:06 I no longer have to remember the full path to the activation script.
5:10 I can just call workon and provide the name of the virtual environment,
5:15 and I automatically switch between them.
5:19 If I want to quickly check something,
5:21 I can create a temporary virtual environment,
5:23 install some packages there, play a bit with it,
5:26 and then they deactivat it and delete it.
5:43 Creating, activating, listing and removing virtual environments are the most popular commands that you
5:49 will be using. But if you go to the website of virtualenvwrapper,
5:53 you can see that there are many more interesting commands.
5:57 Let's go to the command reference,
5:59 and here you will see there is a command to make a temporary virtual environment.
6:04 It will be automatically deleted. When you deactivate it,
6:08 you can see the details. For a single virtual environment,
6:11 you can copy an existing environment to another one.
6:14 For example, when you have a bunch of.
6:16 pip packages already installed, and you don't want to reinstall them by hand.
6:20 You can just copy existing virtual environment.
6:23 There is allvirtualenv command that you can use to run the same command in
6:28 all environments. For example, when you want to update version of some package in
6:32 all the existing virtualenv. To see the list of available commands,
6:38 you can run virtualenvwrapper in your terminal.
6:42 One last thing to keep in mind if you're not using pyenv then if you
6:46 start a new shell, virtualenvwrapper commands will be available for you.
6:50 But if you are using pyenv,
6:52 they won't be. You always have to run pyenv virtualenvwrapper first to initialize
6:56 it. So if you find it annoying,
6:58 you can, for example, add this command to your zshellrc, so it's automatically
7:03 executed when you start in your shell.
7:07 Let's take a look at the list of pros and cons of both the venv and
7:11 virtualenvwrapper the first venv.
7:15 The biggest advantage is that it works out of the box.
7:18 It's a built in module, so once you install python,
7:20 you can start using it right away,
7:23 and it creates virtual environments in the same place where your Project is.
7:27 So, when you delete your project,
7:29 you automatically also delete the virtual environment.
7:33 And finally, if you name it,
7:34 venv or .venv your code editor will automatically detected and start using it.
7:41 On the other hand, if all virtual environments are named venv or .venv,
7:47 it's very easy to confuse them.
7:49 So, you either have to use different names or use the --prompt parameter.
7:53 When you create a new virtualenv,
7:55 and when you want to activate it,
7:57 you need to remember the full path to a specific virtual environment.
8:02 On the other hand, for virtualenvwrapper,
8:05 you don't have to remember in which location you put the activation script.
8:10 You just used this one command workon and the name of the virtual environment.
8:15 individualenvwrapper will automatically detect where it's stored.
8:20 This makes it very easy to activate any virtual environment in any folder on your computer
8:24 As I said, you don't have to remember the whole path to the activation
8:29 script. You just run the workon command and virtualenvwrapper comes with a
8:34 set of commands that makes managing virtual environments much easier. On the other hands.
8:40 If your virtual environments are not stored in the same folder as your project,
8:45 you have to point your editor to a correct virtual environment they wont detected automatically.
8:51 Although, for example, VSCode smart enough to know that virtual environments can live
8:56 in the .venv folder in your home directory,
9:00 so they will try to detect them and they will display you a least of existing
9:05 virtual environment. So even though it's not as easy as automatically detecting,
9:10 which virtualenv to use, at least you don't have to type the whole path
9:14 Just have a list of it the existing ones,
9:16 and you select the one that you want to use.
9:20 And since your virtual environments are separated from your projects,
9:23 if you delete a project folder from your computer,
9:26 you also need to remember to remove the virtual environment.
9:30 This is not really a big deal,
9:31 since virtualenv don't really take that much space,
9:35 I usually remove them when I forget what was the purpose of a specific virtualenv
9:38 In the end, it's up to you to select which one you want to
9:42 use. I'm using virtualenvwrapper because I find it much more convenient to not
9:47 have to remember in which folder I placed a specific virtualenv.
9:51 But venv much easier to start with,
9:53 so it's very common to use it at the beginning,
9:56 and when you feel comfortable with using virtual environments,
9:59 then maybe you can switch to a separate tool.