How To Create & Activate A Virtual Environment In Visual Studio Code (Python 3.12)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you how you can create a virtual environment and how you can activate that virtual environment so that you can use it in Python to install external modules and we're going to be doing this in Visual Studio code but the concept can be transferred to any code editor so first of all I have a folder called python folder and I'm going to create a new file Insight here and this one's going to be called main. Pi so that of course we can write some code and as my interpreter I'm using Python 3 3.12 but you can select any interpreter you want now inside here we can do something such as print hello and if we try to run this we're going to get hello as an output in our console but what if we want to use an external module such as the requests module or if you're building a Discord bot the Discord module well you might be tempted to just type in import requests and then use it as usual but that's not going to work because it does not exist in our python project it cannot find it and it's not part of vanilla python as you can see if we were to run this as is we're going to get a trace pack and that's going to provide a module not found error because there's no module named requests so I'm just going to clear that for now because we need to create a virtual environment which is going to contain this module as soon as we install it and the way we can do that is using the following commands on Windows it's slightly different so I'm going to insert a screenshot right here but on Mac we can type in Python 3-m virtual environment and the name of it is going to be VNV and that's going to create for us a virtual environment as you can see in our python project folder we now have this virtual environment that has a bin and so on but we still did not activate it and you can tell that we have not activated it because we are still in the base so now I'm going to close the sidebar and I'm going to show you the command that we need to use to actually activate this virtual environment and once again the command for Windows is slightly different so I'm going to just insert a screenshot right here so if you're on Windows you can just use that but on Mac we need to type in source. VNV Das bin D activate and you'll see that this time we're going to have a VNV in front of the base which means we are effectively inside our virtual environment or we are effectively using that virtual environment which makes it easy to do things such as pip install requests now let's try to run this code again by typing in Python 3 main.py and it's going to work perfectly fine now let's actually try to use it because all we did inside this line of code here is import requests but we might actually want to display that it works by using it so here I'm going to create a request which will equal requests. get and I'll insert my website and all we're going to do is print the request. status code now let's try to run Python 3 main.py once again and it did nothing cuz I did not save it but now if we run it it's going to give us a 200 status code as you can see in the console it used the request module to do that also another thing you might want to remember to do if you are just running your code again and again in Visual Studio code is to go down to your interpreter because as soon as you activate your virtual environment you're also going to have a new interpreter to use and if you tap on this new one it's going to hold the virtual environment which means now the errors are going to go away and you can directly run your script without any errors but that's really all it takes to create and activate a virtual environment in Visual Studio code but do let me know in the comment section down below whether this tutorial was easy to follow or whether you still have issues I would love to hear about that in the comment section down below but otherwise as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 28,690
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: hC5rfoIY8nU
Channel Id: undefined
Length: 4min 7sec (247 seconds)
Published: Thu Oct 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.