How to create a Python Virtual Environment (Beginner Friendly)

Learn to create and use Python virtual environments to keep project packages isolated from your system. The tutorial covers creating a folder, running `python -m venv`, activating it in your terminal or VS Code, installing packages like pandas, and deactivating when done.
Chapters
So you've heard about virtual environments, or maybe you never even heard about virtual environments. So what is a virtual environment? In today's tutorial or demonstration, I want to be able to explain to you what a virtual environment is, why you need it, how to set it up, how to use it, and finally how to deactivate it once you're done using it. What is a Python virtual environment, right? So first thing I did, I went to the Python documentation. So you can always reference documentation for whatever software you're trying to build or interact with. So on the Python documentation, it mentions or it defines virtual environment. Basically, the gist of it is you're using a Python virtual environment so that the packages that you're going to install to use in your project doesn't interfere with the system packages or applications to actually use the packages. So you If you need a— maybe for example, I'll use a very interesting example. So anytime you're going to use pip install something with Python, if you're running an environment, so maybe your system already has a package that it's using, other apps are using in your system. So if you— maybe you need a 0.5 and maybe your system uses a 0.3, for example. So if you just install it in your bare system without using virtual environment, it's going to cause a clash with the other apps. They're trying to figure out which version to use. So the best way to do this is you create an isolated box, which is the virtual environment, where you can install your packages in there and use it strictly for that project that you're building. So that's the gist of a virtual environment and why you need it. And I really highly encourage you, anytime you build a— or you're working on a Python project that needs pip, which it's mostly all the Python projects that you can ever build, if you're going to import anything using pip or install anything with pip, I highly encourage you to use virtual environments. Makes your life so much easier. So let's get to the next topic, which is how do you create this virtual environment? The steps that I usually take to create a virtual environment. First of all, I always go to my command prompt and I will cd, I'll move into my D drive where I usually have my projects based in. And in here I'll create a directory where it's going to hold the project. So this will be cd, I'll make the project as test project. And I will also cd into my test project folder, uh, directory in this case as well. And now we are in the test project directory. So from here, this is where we want to build our project, and we also want to host our virtual environment in this directory as well. So to create the virtual environment is really easy, just this one command. All you have to do is python -m space venv, just meaning virtual environment, And then after that, you can name it whatever you want to name it. You can name it venv as well, or env for environment, or whatever you want to name it. In this case, let's just name it test, right? So test. And it's going to take a few minutes or seconds for Windows, at least from my experience, to be able to generate that and build that out. And we can navigate to that folder here and see it loading up that So for now it's empty, but it's going to be loading up the folders that's required in here. So let's wait and see. So see, test is built, and this will hold the virtual environment. As you can see here, it has a bunch of files, and it's finished creating the virtual environment. And you can see it has a bunch of files, has include, library, has the site-packages, script. And this is where you'll find the Python version in here, and you'll also find the activate one. So the activate it will activate your virtual environment, and we'll get to that on how to activate. The next thing I'd like to do is go to Visual Studio Code, and from there we can, we can actually test it out. And to do that, I'll just do code space full stop and enter. That's just a shortcut for you to get to Visual Studio Code, and it will load up. Here we are. And from here you can see it already has that folder within our project. So you can come here and create whatever other projects you want to build here. But this just shows you how to use this. And we haven't even activated it yet. So to activate it, let me use a terminal here from VS Code as well. And we're already in the project directory that we want to be in. And so to activate the virtual environment so that you can start installing your packages that you will use within this project, All you have to do is navigate to this folder right here using the terminal, and you will be able to activate the virtual environment, and you will see when that happens. And let's do that. Let's go to test, and let's do scripts, and then let's just do activate and click enter, and boom, there you go. Now you have test, and this test in front of like the directory name and where what folder you're in right now. It just shows that you're now using this virtual environment, it's activated, and you'll be using— so if you do any pip install pandas or something like that, then it will just install this pandas into this virtual environment and it won't install it into your system. So for me, I already have it in my system. If you didn't have this, then it would tell you, you know, requirement already satisfied or whatever, or you just update it to the newer version, and that's not what we want to do. For the most part, if you do have programs that are using the older versions. So this is very important. So if you have different programs that maybe some programs, even current programs, don't use old packages, so you might want to use an older package, or it requires Python, I don't know, 3.6, right? And right now you're 3.10. So you need to create a virtual environment so that you're able to use Python 3.6 without interfering with your 3.10. So that's exactly the importance of this. And you can just install the majority of the stuff that you need and then build your project. And once you're done, you can also deactivate this virtual environment. And to deactivate it, you just type deactivate. Uh, sorry, type that in there. So yeah, just deactivate. Uh, sometimes you can just do deactivate tests, the name of the virtual environment, but in this case, if you just type deactivate, it's going to deactivate that virtual environment. And now we are back to our old system where if you pip install anything, it's going to install it within our directory that it's meant to be within Python. So this is a very nice, easy way to, to get that working. So if you have any issues or you, you know, you want to post anything, let me know in the comment section. If you like this, if this helped you in any way, give it a thumbs up and subscribe as well. Um, thank you guys.
More from Tony

Summarize Videos and Research Companies with CrewAI + Gemini Models [Quickstart Guide]

How to Make Your AI Agents More Reliable with CrewAI Task Guardrails (Step-by-Step Tutorial)








