Try this way to create Python Virtual Environment in folder and on system | Step by Step DEMO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
darker from automationstepby-step.com and today we are going to go very basic step by step and from scratch we are going to learn how we can create a python virtual environment and we are going to do it in two ways the first way is we will learn how can we create a python virtual environment within a folder within a project folder and the second way will be how to create a python virtual environment at a global location and then connect it with our project folder this is going to be very easy and very interesting so before going to before looking at the steps of how to create the virtual environment let us see what is a virtual environment and why do we need virtual environments so here uh let me go here and show you let us say let us say this is our system here okay and here within our system we will install Python and that will be available in some folder inside our system so let us say we have our python here on our system okay now when I create any python project so let us say this is my python project I'll call it as project one and I can have more projects here on my system I will call this one as project two now whenever we have to use any python libraries any python packages we will install those python libraries using the PIP command so let's say if I run some command on my system let us say pip install and the library name let us see selenium so when I do this all these libraries all the python libraries that I will install will go to the default python location that is the global Universal location of python on the system so whether I am using the libraries for project 1 or project 2 or any other project on my system all will go here at this location okay and this may not be a very clean process because all the libraries all the all the all the projects libraries all the python packages are all present here on this Global python location so if I have a way where I can keep the python libraries needed by project one separately and project two separately and every Project's python libraries and all the python packages separately it will be a very clean system it will be a very clean way it will keep the dependencies for each project isolated and it will not interfere with any other projects libraries or the global libraries so that is why we create virtual environments so virtual environments are you can consider as as a folder where we keep every projects Python dependencies and liabilities separately now there are two ways to do that I can either create a virtual environment for each project within the project folder and I can keep all its python libraries and packages inside this so I can do like this so project one will have a folder and all its python dependencies here project 2 will have it here so it will be within the project folder or I can create virtual environments at a global level that is wherever our python is and I can then create all these separate environments and then connect to the project so let's say this will be for project one this will be for project two and so on okay if I show you some images python virtual environment and I go to the images here and here also you can see this is one image so you can see here as well we have different environments created and we can also in this way if you have multiple installations of python on your system and let's say you want to use uh some particular installation let's say python 2 for a particular project in Python 3 for a particular project in Python 3.10 for a particular project even that is possible using virtual environments so we can create different virtual environments and keep their Python dependencies and requirements inside those environments or inside those folders okay so here the advantages will be it helps in isolation of dependencies we can keep all the projects dependencies separate from every other project and system it is easy to reproduce the exact environment and dependencies needed for the project if we take our project to a different system or a machine and it will it is a very lightweight and easy to create and manage if we use Python virtual environments okay so now let us see the steps so first we will see how we can create a python virtual environment within a project folder okay and the prerequisite is python should already be installed on your system now just in case you you do not have python I I have a different video for that I will put the link of that video in the description of this video so you can find that and just to check if python is there on your system you can go to your command prompt if you are on Mac you can go to terminal and then say python space hyphen hyphen version and this will show you the python version if it is installed and you can also check pip version if space hyphen hyphen version pip is the package manager for python so it is there on my windows if you are on Mac you can check on the terminal so here I say python space hyphen hyphen version and pip space hyphen hyphen version so just in case you do not see the output of these commands I will keep a link of the video where I have shown how to install python on Windows and Mac and you can check that from the description of this video okay then now we will create a new folder and then go to the command prompt or terminal and go to the location of that folder so let's do that now here on my windows I am creating a new folder you can create anywhere I am just creating here on the desktop and I'm just calling it as my project one so you can imagine this will be my project folder in real world and now I have to go to the location of this folder on command prompt or terminal so on Windows one of the shortcuts is you can open the folder and go to the address bar here and then here just say CMD and hit enter it will open command prompt on the same location that is the folder location so that is one way otherwise you can just copy the location of the folder so let me just copy the folder location and copy it and then I will go to the command prompt and say CD and the location of the folder now just see in my case I was already on C drive on my command prompt and the folder is also on the C drive that is CE users administrator desktop so that's why I did not have to change the drive just in case you create your folder in on a different Drive first change that change the drive let's say if you want to change to e Drive you can say e colon and then give the location of the project after saying CD CD means change directory space and then the location of your project folder and then hit enter I will come back to my C drive and also clear the screen okay so now I am here now the next step is to create a virtual environment I will say python minus m v e and B and the name whatever I want to give the name of my virtual environment okay so I will say python minus m v e and V and then the name so let's say I will call it as uh you will see as I will create the virtual environment you will see the folder of the virtual environment coming here in the project folder so I will say my env1 you can also give some meaningful names like environment for project one or something like that I am saying my e and B1 and hit enter and you can see the folder has come here okay and it will take a few seconds so it will create the environment it is created and you can also see the folder has come here and it will have all these scripts Library folder so these are what you will also see in your python project a python folder that is when you install python so if I just show you let me show you very quickly I have installed my python on my D drive so while installing python I selected a location of d Drive and here this is my python installation and you can see lab scripts all these folders are here as well so if you install anything any python library at a global level it will go here in the main installation of python on the system and now when we are using virtual environments and when I activate this virtual environment and then do any installation all that libraries will go inside this folder for this particular project okay so now this is done I have to now activate my virtual environment so that it is activated and then I whenever I'll run any python command like installation of any libraries it will go in this particular environments folder so if you are on Linux or Mac you will say my environment and they will be a bin folder and they will be activate file there for Windows in whatever is your environment name there will be a scripts folder and then there will be this activate dot bat file I can also show you this manually if I go to my environment this Scripts and then you can see this activate dot bat or activate anything will work so I will now do that I will say my EnV Scripts I am pressing tab so it is auto Computing so as I say SC and press tab on my keyboard it is auto completing and then activate hit enter and now you can see this environment is now activated and the way to check that is you will see this environment within brackets at the start of your line in the command prompt okay so now I am in this environment and now whenever I'll run pip install any library or any command it will all go in this particular environment or in other words it will go to this environment folder okay so for deactivating you can say deactivate but before doing that let us try to add something so you can see if I go to uh lib and site packages you can see as of now all this is here but if I install any new python Library I will say pip install selenium and let us see what happens so you can see it is installing selenium and it will come here you can see all these new folders and files have come here you can see all this selenium and all the required dependencies of selenium all the files have come here within this virtual environment or this folder okay so now for this particular project all the libraries all the python requirements will go in this particular folder itself and we can keep all these dependencies separate from every other project and from the global python libraries and dependencies okay so we have already seen this the name of the environment will be available like this and this will work on any other ID as well for example if you are using vs code you can do the same command you can run the same commands on the vs code terminal and this will work as it is okay so we have already done this we have checked this now if we install any python library or package it will go to this particular environment only okay and now uh one other important thing is uh I can now if I want to save all the dependencies to a single file I can just say freeze or pip list and give the and push all the list of the required libraries to a text file so we know this uh let me show you if I say pip list it will show me list of all the python libraries for this virtual environment I can also say pip freeze it will also show me the list of python libraries okay and now you can also see selenium is here which I have installed just now I can say pip freeze and put this in some text file req1.txt so you will see the text file is created here and if I check this all my project dependencies have come here so it becomes very easy to see all the dependencies and then if I take this project to any new system any new machine I can just use this command pip install minus r and give the file name where I have copied all the dependencies so it will install all the dependencies in the new environment on a new system or a new machine okay so this was how we can create a python virtual environment in a project folder now the next steps are if you want to create the python virtual environment at a global level and then connect it to your folder for that these are the steps let me show you I will create new folder now I will exit from this one more thing I forgot to show you is if you want to deactivate the environment you can say deactivate okay and now you can see the environment is now deactivated it is not here and if you again want to activate you can again run the activate uh call the activate script so which is my EnV whatever is your environment name and scripts and scripts and activate so this will again activate the environment so I will just deactivate for now and in fact I will exit from this command prompt I'm going to create a new folder and I will call it as my project to okay this is a new folder I have created let me open this as well okay now for creating a particular a virtual environment on the system and then connecting to the project first we will have to install the virtual environment package using pip so I will say pip install virtual environment or virtual ENB I will open a new command prompt and I will say pip install virtual now this will go to the global python dependencies virtual EnV okay this is already there and the next thing for Windows you can install a Windows wrapper virtual environment wrapper hyphen win but sometimes it is not required so I'll just go without it and you can check if you need it you can get it using this command I will put all these nodes all these commands in the description of this video okay now I have to go to the command line and go to the folder of my project so let me just copy the location of my project and I will go here I will say CD and the location of my project let me clear the screen okay I'm here now I will use the command MK virtual EnV and the environment name so I will say MK virtual EnV I will say my env2 okay and it says created virtual environment and all these details okay now if you want to see a list of virtual environments which are there on your system the python virtual environments you can say LS virtual EnV and this will show you all the virtual environments now in my case you can see some more environments because all these I created earlier and this is the latest one which I have created now okay and also you can see I'm already on my my env2 virtual environment okay so I when I created this a virtual environment I was connected to my virtual environment automatically okay just in case you want to switch to any virtual environment which is there on your system you can say work on and then give the environment name let us say I say hello world which is this virtual environment here so I'm seeing work on hello world and you can now see the virtual environment is now changed here okay I will again say I'll switch back to my env2 I'll say work on my env2 and now I am on my in V2 okay let me clear the screen so now I am on my virtual environment and now we know to deactivate we can either um exit from the command line or terminal or we can say deactivate command and it will deactivate the environment okay now we have to set or bind our virtual environment with our current project directory or current folder so for that we say set project dir and Dot so I will say set project dir and Dot and hit enter you can see it says this is now the project directory for this virtual environment okay so it is now set and uh let me see I think I changed when I changed it also changed the location of my folder I will have to go back to the location of my project to folder and then set it so let me do that let me deactivate okay and now I will say C colon to come to my C directory and yes I am now on my project folder or I will say CD and now I will say LS virtual EnV I will say work on my e and B2 okay now okay I believe my inv2 was already created let me go back I think that is the issue let me go back I will deactivate okay now now I am on my project tools so I think I had to change it from here now I am here and now I will say set project dir Dot and now it is say it says this is now project 2 is now the project directory of this virtual environment that is my env2 okay so this is what I needed okay so just take care of this what I just did okay you will also have to change the location in case it is changed automatically and also now so if you can see if I do any installation now let me clear the screen if I say pip install selenium or let me say playwright so it will get installed on this virtual environment which is bound to this particular project folder okay and we can also check this if I go to that particular virtual environment folder and for that I can say which python I think which python works on Linux and Mac here I will say via python so here I can see the list of virtual environments which is this the location of the virtual environment folder so I'll go here on my system and here when I go to lib and site packages here I can see playwright is installed here okay so now this is how we create virtual environments at a system level and then connect with our project folder so I hope this was very useful for you if you have any questions you have any doubts you can let me know in the comment section and I will reply to you I hope all this was very useful thank you for watching and never stop learning
Info
Channel: Automation Step by Step
Views: 12,723
Rating: undefined out of 5
Keywords: how to create python virtual environment, what is a virtual environment in python, what are the advantages for virtual environment, how to connect a folder to a virtual environment
Id: oN0cISyzWe8
Channel Id: undefined
Length: 22min 48sec (1368 seconds)
Published: Thu Feb 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.