PyCharm Virtual Environments (venv) Explained! | 10 Min. | Updated 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everybody in this video I'm going to cover how to use Virtual environments in pycharm there are two common ways to create a new virtual environment the first is with a new project and the second one is creating one into a project that already exists so let's start off with the first method so we want to create a virtual environment with a new project the way to do that is to go to file and then new project and in the window that pops up you'll see this small button over here which says new environment using virtualens so make sure that that check mark is actually checked and you'll also see two additional options below that the first one is to inherit Global site packages and that will make the packages that are installed globally on your computer available to your project and then the check mark below that allows you to make your virtual environment which you're creating available to other projects we are not going to require either of these so we can simply go ahead and press create and once you've done that you will see that this new project is being created and you'll have to wait a moment for it to load and as soon as it has stopped loading you will notice a few things the first one is that in your project directory you're going to see an additional folder called venv which is short for virtual environment and the second thing that you will notice is that in your terminal you'll see that there's going to be this vent in Brackets before the path which indicates that the virtual environment is active so now we know how to create virtual environments for a new project but now let's try the second method so creating a virtual environment for an existing project so right here I've brought up a blank project that does not have a virtual environment and it's easy to see that because within the main directory we don't have the folder virtual environment and in addition in front of the path in the terminal we don't have that venv in Brackets so to create a virtual environment for this project what we need to do is we need to go to file settings and then within the settings we go to the python interpreter over here and in this window you will see on the top right hand corner there's going to be this small gear I icon which you want to go ahead and click on and then press add and in the window that pops up you'll see that we have the possibility to create a new environment so then we can press on OK and after that we can press on OK again and once that's done you'll see that we have a new virtual environment within our existing project as you can see now on the left hand side we have this new folder virtual environment which is great but one thing that's missing is that the virtual environment at the moment is not yet activated but activating it is pretty simple so all we need to do is we need to be in the terminal and within the terminal we first want to have a look at the folder structure that we're currently in so we can do that by typing the command LS and that will show you all the folders that you currently have in this directory and you'll be able to see that one of them is called vev which is the virtual environment so you want to go into that directory so we can press CD which is short for change directory and then v e and V to go into the directory now you'll see that the path has extended by what you just wrote in because now we have changed the directory into the virtual environment next you can go ahead and press LS again which will list up all the individual folders within the virtual environment folder and you can see that one of them is called scripts and you want to go into the scripts so we change directory again and go into these Scripts and then we go ahead and list all the individual files and folders that are in the scripts folder and you can see that there are quite a few now I do want to point out that we can also have a look at the file structure within the file Tree on the top left over here but I just want to make sure that you guys are familiar with using the terminal because when you're using virtual environments you'll be using the terminal quite a lot it's quite convenient that the script that we need to activate the virtual environment is actually called activate but you can see that if I scroll through the files within the scripts folder there are actually quite a few called activate for example there's this activate then there's this activate.bat there's this activate dot fish activate dot Nu activate PS1 and so on and so forth in my case you can see that in the terminal if I look in front of a path you can see that it says these initials PS which is short for Powershell that is exactly why I'm going to be using the activate dot PS1 to activate my virtual environment so if I go ahead and type activate dot PS1 into the terminal and then execute that you will see an error message pop up which says the term activate.ps1 is not recognized and most people get confused because they only read the red part of the message but not what it says below that because if you read a couple of sentences further then you'll see that it says if you trust this command instead type dot slash activate.ps1 so if this message is also popped up for you you can go ahead and copy the dot slash activate.ps1 and paste it in and then press enter again and now you will see that the virtual environment is active because in front of your path name you will see the ven within brackets which shows you that it has been activated successfully I also want to point out that in some rare cases if you are on a Windows machine you might get an error that says something along the lines of activate.ps1 cannot be loaded because running scripts is disabled on this system if you get that error then all you need to do is open up Powershell in administrator mode and enter the following line of code and once you do that and come back to pycharm and it should work perfectly and another thing that I want to point out is if you want to deactivate the virtual environment all you need to do is write deactivate into the terminal and then press enter and you will see that the ven in Brackets before your path is gone but let's go ahead and activate it again because now we're going to take a look at how we can install some modules into the virtual environment installing libraries when a virtual environment is active is actually quite easy all you need to do is write pip and then afterwards write the name of the module that you want to install so if I write pip install Pi game and then press enter you will see that the module will be installed but the question is where can we actually find this module within the virtual environment folder it's actually quite easy to find if you look into the file structure and then go into the file called lib which is short for libraries and afterwards go into the subfolder called site packages you'll see that you now have the module Pi game installed within the virtual environment alternatively you can also write pip list into your terminal and press enter and then you will have a list of all the packages that are installed and here you'll also see that one of them is pi game which we installed a moment ago but I want you to be aware of the fact that this package is only installed within your virtual environment so if you deactivate your virtual environment by simply writing deactivate into the terminal and then pressing enter you will see that the ven in Brackets in front of your path is gone again and now if I write out pip list you will see some other modules here because it is referring to all the global site packages that I have installed and the globally installed packages might be different to the ones that you have installed locally in your virtual environment here for example it is easy to see that I don't have Pi game installed so it is not Inc included within the list of global packages that I have installed on my computer but it is installed in the virtual environment to show you that more clearly we can go ahead and write pip list and then minus V and you'll see that we don't only receive the packages that are installed but we also see their location and here you can see that the location I'm currently looking at is the global one which is in my case python39 slash lib slash site packages but if I now go ahead and reactivate the virtual environment by writing dot slash activate.ps1 and then execute the same command again so pip list minus V you can see that I'm going to be looking at a completely different file when I have the virtual environment activated because now you can see in the terminal I'm looking at C users Max pychon projects python project 2 slash vent slash lib slash site packages which is a different path to the one that I was looking at before so to sum that up in one sentence whenever you have the virtual environment activated you are installing all packages that you install with Pip to your virtual environment locally but when you have your virtual environment Switched Off You are installing packages globally onto your machine and the difference will be that the local packages will only be available to your project and the global packages are going to be available across any project that you have on your system alright that's going to be it for this video If you enjoyed it then help me out and leave a like on this video leave a comment on this video And subscribe to this Channel and we'll see each other in the next video
Info
Channel: Max on Tech
Views: 73,829
Rating: undefined out of 5
Keywords: pycharm, venv, virtual, environment
Id: 2P30W3TN4nI
Channel Id: undefined
Length: 10min 46sec (646 seconds)
Published: Wed Jan 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.