Writing every week about programming, new technology and books.
What is virtualt environment in Python?
What is the purpose of a virtual environment?
Let's say you need to work on the project and the requirement is that you use Django 3.0 version (Python Web Framework). Ok, no problem, just install Django 3.0 version. But what if at the same time you are already working on a project that uses Django 2.2 version? How can you keep both versions on your computer at the same time? This is where the virtual environment comes in. The purpose of the virtual environment is to enable users to isolate their projects, from others so if on Project A you have Django 2.2, and you want on Project B to have Django 3.0 you can do that without affecting your first project.
How to make a virtual environment?
In order for the virtual environment to work the only thing you need is Python installed on your PC. If you want to see how to install Python you can read this blog. Ok, enough talk let\'s do some work.
First, you need to pick a folder that will be your virtual environment and then open it with the command prompt.
To create a virtual environment you need to type:
- myfirstvenv is just the name of a virtual environment you can name it however you want it.
Now, the virtual environment is created and as proof, you can see a new folder with the name you gave to the virtual environment. But it is not activated yet. In order to activate you need to type:
If you successfully activated your virtual environment on the left side of the command line you should see the name of the virtual environment.
(MYFIRS~1) C:\Users\nikol\OneDrive\Radna površina\Python Automation>
When you type the command:
Pip freeze is going to show all packages in the current environment. You will see that nothing will be displayed. That is because we have nothing installed in our new virtual environment. If you want to install for example Django 3.0 version you can type:
pip install django==3.0
Now if you type again:
The result is going to be:
asgiref==3.2.6 Django==3.0 pytz==2019.3 sqlparse==0.3.1
The other three packages are by default with Django.
Another important use case of Virtual Environment
In order to run code, downloaded from the Github or any other platform is to know versions of packages they used. So, when you are pushing your code to the web you need to leave a list of all the packages you used and their versions. This is done by saving all packages and their versions inside the requirements.txt file.
You can do this manually by typing pip freeze and then copy-paste result inside requirements.txt file. Or you can do this in one line of code:
pip freeze > requirements.txt
This will get a result of the pip freeze command and store it inside requirements.txt. Now when you find the project on web that you want to run locally all you need to do if to find their requirements.txt file and run the command:
pip install -r requirements.txt
Understanding the virtual environment when using a Python is a must. It will make your life much easier and others using your code. It will be hard at first but knowing this will help you in the long run.