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


Talk Python's Mastodon Michael Kennedy's Mastodon