If you'd prefer to view the youtube video for this topic, please click here.
What Is a Python Virtual Environment?
So you create a super awesome python program and want to share it with someone. But it turns out that your code requires libraries that are on your computer, but not on other people's computers. That means they would have to install the libraries on their computers, but this creates confusion about which libraries are necessary, how they install it, which version of they need, etc.
The solution to this is to create a Python Virtual Environment. This is similar to NPM for Node: if there's a python library that your project requires, you can install that library locally (in the project folder) instead of just doing a global pip install on your computer.
How To Create a Python Virtual Environment
So how do we use a python virtual environment? First let's make sure that we have python and pip installed on our computer. (The following commands will [probably] work for linux and Mac.) In a terminal, type:
Next, let's make sure we actually have the virtual environment library/command available to us. Use pip to install it:
Good? Good. Now we have all the tools we need to make a python project and set up the files in our virtual environment.
Let's pretend that our project consists of a single python file called main.py with the following code:
Two simple lines of code. Now let's try to run the python file. In the terminal:
Does it work for you? It might, depending on whether or not you have the scapy python library installed on your computer. If you don't, then you'll get an import error.
Normally, we'd install the library onto our computer globally using "pip install scapy", but instead, let's install into the local project by using a python virtual envionment. Type the following command into your terminal:
That will tell your python virtual environment command that this project requires scapy.
What happens if we try to run the project now?
Actually, the same import error will occur. You installed the library through pipenv, so now you need to run the python file(s) through pipenv so that pipenv can include the required libraries.
Now we can see the Hello World in the terminal. This is because pipenv included the scapy library so that the import line didn't give an error.
When To Use a Python Virtual Environment
So when should we use this? Honestly, for every project that we intend to share with anyone, we should include a python virtual environment. That way anyone who receives the project won't have library compatibility issues.
If you'd like to watch the youtube video that walks you through going through these commands, please watch the video embedded below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Hi, I'm srcmake. I play video games and develop software.
Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.