Loading...

What is Python virtual environment?

What is Python virtual environment?

If you are a python beginner, you must have come across the term Python Virtual Environment and wondered What is Python Virtual Environment? While this blog is all about it. By the end of this blog, you will know what it is and how to work with it. But let’s first understand you you need a virtual environment. So, let’s begin!

If you’re just getting started with python, you could have come upon a common beginner’s problem — your programs stop working after some time. You might also not understand in which folder your python packages are stored or how to manage distinctive variations of python versions between different projects. But what if those packages are not compatible with each? That’s wherein Python virtual environments come into play.

What is env?

Python virtual environments is an isolated python environment for separate python versions to operate on your pc. What this indicates is that you could have separate virtual environments with separate variations of python and python packages for each of your projects. Consequently, you can set up or eliminate python packages on one environment and it’s going to now not have an effect on some other projects you may have.

The packages or libraries installed in every virtual environment are present in that virtual environment only and no other. Moreover, this virtual environment is separate from the files stored in your main computer system.

Set up python virtual environment

Install the python virtual environment package.

>>pip3 install virtualenv
Code language: PowerShell (powershell)

I prefer to place my virtual environment at the root of my projects. This makes it a bit easier for me to track them. However, you can put all your virtual environments in the same folder.

Let’s create a new directory named “new_project”. And enter that director:

>> cd new_project
Code language: PowerShell (powershell)

Create virtual environment

In this blog, I will use venv to create a virtual environment. Some developers also use virtualenv to do so but from python v.3.3, venv has virtualenv built-in.

Create a virtual environment using the following command:

Windows

>> python -m venv venv
Code language: PowerShell (powershell)

macOS/Linux

$ python3 -m venv venv
Code language: Shell Session (shell)

Here, in windows, you can write only python but in macOS/Linux you need to specify the version you are using like python3. Then, -m env is an argument that tells python to run a virtual environment. And venv at last is the name of our virtual environment, you can name it by your choice.

Below is how the folder structure looks after creating venv.

virtual environment folder

Activate virtual environment

Note: make sure you’re in the same folder/directory where you created the virtual environment. (new_project in this blog)

Windows

>> venv\Scripts\activate
Code language: PowerShell (powershell)

macOS/Linux

$ source venv/bin/activate
Code language: Shell Session (shell)

If you see a (venv) in your terminal, your virtual environment is activated successfully.

command prompt

Install Packages in a virtual environment

Windows

>> python -m pip install <package-name>
Code language: PowerShell (powershell)

macOS/Linux

$ python -m pip install <package-name>
Code language: Shell Session (shell)

This is the common command to install packages in a virtual environment. This will install the package isolated in your virtual environment.

Also, in macOS/Linux as we have activated virtual environment using python3 so we can don’t have to use python3 every time. You can use python it instead for further commands, it works the same till the environment is active.

Deactivate virtual environment

Once you’re done with your project or want to get out of the virtual environment by deactivating it.

Windows

>> deactivate
Code language: PowerShell (powershell)

macOS/Linux

$ deactivate
Code language: Shell Session (shell)

After executing the following command, the (venv) will clear from the terminal and you will exit the virtual environment successfully.

Benefits of using a virtual environment

Using a virtual environment can be very useful and beneficial because of its several use cases, some of which are:

  • Your development environment is contained inside your project, it will become remoted, and does not intrude together with your system hooked up to Python or other virtual environments.
  • You can create a new virtual environment for a different Python version.
  • You don’t require admin privileges to download packages in venv.
  • It becomes quite easier to build up your project as an application package and share it.
  • You can easily create a list of dependencies and sub dependencies in a record, to your project, which makes it easy for other builders to copy and install all the dependencies used inside your surroundings

Conclusion

Concluding, Python virtual environments provide the capability to isolate your Python improvement initiatives from your machine hooked up to Python and different Python environments. This gives us full control of the project environment and makes it easily reproducible.

That’s it for today! Thank You for reading.

Sharing is caring

Did you like what Prabal Gupta wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far