Why should you use a Virtual Environment on Python?

If you use Pycharm you must have noticed the Venv feature. Okay! now the Venv (Virtual Environment) is not specific to only Pycharm, it is a python feature and Pycharm happens to understand and provide this feature built into the software.

If you are a long time python user, most probably you already know about Virtual Environments. Although still if you want to know why you should use a Virtual Environment give this article a quick read!

What is VENV?

Venv or Virtual environment is a feature that lets you run different programs independently. In other words, Virtual Environment lets you install dependencies related to a program only for that specific project. That means you can install a package in the virtual environment rather than having to install it on your root system, and that package can be only used for that specific project.

That means a Virtual Environment is used to isolate one python project from another one, well from the whole system actually.

How is Venv used?

Well, a virtual environment can be used in many ways, for instance, if you want to just test out a random python package and are not really sure about installing it in your root directory for whatsoever reason. You can create a project using a Virtual Environment and install the packages there and discard the whole project after testing.

Or if you want to test some temporary projects and it needs some packages that you are not sure if you are gonna actually use it again in future, it is just better to use a Virtual Environment for that project as u won't have to install unnecessary packages on your root system and after the testing is done you can just delete the whole project.

Although, this doesn't mean you can't use a Virtual Environment for large projects. It is actually a good practice to use a virtual environment.

Advantages of using a Virtual Environment

  • Using a virtual environment gives you more control over your project.

  • Even with large projects, a Virtual Environment makes it easier to manage and track installed packages.

  • Your main Python package directory doesn't get flooded with unnecessary packages.

  • You can actually use different python versions on different projects. (If for any reason you are planning to do so)

  • As the packages installed are particular for that single project if someone else wants to run your code, they can easily figure out which packages are required for the project. Better if they just copy the whole directory it will run fine without having them to install any separate packages.

Disadvantages of using a Virtual Environment

  • Well, there aren't actually many disadvantages of using a virtual environment, the only one I can think of is space. It will consume more space then just creating a project in the existing environment as it will create a copy of the actual python installation in the project directory. But this isn't really a big issue as it will use up a couple hundred Megabytes at most.

Conclusion: Should you use a Venv?

Well, I don't see any reasons why you should not use a Virtual Environment unless your project is not package dependent at all! I mean there is no point in using a complete different copy of the pre-existing files if you are not using any special packages.

Although if it a big project which requires a lot of different packages using a virtual environment can make life extremely easy as it gives you more control over your files and lets you manage your packages more efficiently.

245 views0 comments
  • Facebook
  • Instagram

©2020 by thebytewise. All Rights Reserved