Top 10 Python venv Commands You Should Know (Cheat Sheet)

Python venv commands that will change the way you use virtual environments on Windows and Linux.

0

Top 10 Python venv Commands

Venv is used to provide support for creating virtual environments which are light in weight with their very own site directories, optionally isolated from system site directories. These are new in version 3.3 source code: Lib/venv/. It is different to virtualenv. You can use any of the below top 10 Python venv commands to be more efficient in managing and creating isolated environments for your work so you are more organized by being able to manage it.

Why venv is used a lot in Python projects

It helps to create a Local Environment (not system-wide) that is specific to the project on which you work. Therefore, as you begin to work on different projects they would have different dependencies like different versions of Django, so you would need to have a different virtual environment for each.

It is a self-contained directory that carries a certain version of Python and some additional packages as specified by the user. Moreover, you can also install or uninstall any library you want by using “pip” once the environment gets activated.

The version of python (the one installed by default on your OS shall be included unless and until you specify a different one), more libraries and scripts installed on the virtual environment are isolated completely from the ones which have been installed on your machine or any other.

Without any doubt, you must know that Python comes with a virtual environment manager which is known as “venv” for version 3 “venvlenv” for version 2. Now let’s see how to showcase the use of “venv” which is regarded as the lowest tool to manage Python packages within the environments.

Examples of venv uses:

These include Email, web-based document sharing applications and chat. All you need to do is put them in a networked common operating space.

Now, we have compiled for you some of the best and most commonly used commands that shall help to create, use as well as manage Python virtual environments, follow through to know more.

ALSO SEE: 11 Python Language facts that are true and not scary.

Top 10 Best venv commands List for Python (Full Cheat Sheet)

Python venv commands cheat sheet

Before you continue, either on Linux or Windows, make sure Python is installed on your terminal or CMD. Otherwise, you can follow our article to install it.

Allow venv Access to other System Site-Packages

All the dependencies which get installed in this are isolated from corresponding packages installed in the actual system or some other environment.

For any purpose you intend to give its access to system site-package dir, then all you need to simply do is specify “–system-site-packages” flag in order to instruct “venv” to do so. Keep in mind that this is not one of the recommended practices and you can make use of this in time of need.

[email protected]:~#python3 –m venv /path/paul_virtual --system-site-packages

If you are not in the mood and want to either skip or upgrade “pip” (if you don’t know it is that default package manager) then it can be done by passing “–without-pip” flag. You can use apt-get method.

[email protected]:~#python3 –m venv /path/paul_virtual --without-pip

Create directories with specific target

You can also create a directory that would only work for a set virtual environment.

[email protected]:~#python3 def create(self, env_dir):

Activate Virtual Environment in Python with one command

As you have created one now you need to tell your OS to make use of it by first activating it. For this, you need to call out the “activate” script which is present under bin/ sub-directory there in the created tree structure of the environment.

[email protected]:~# source paul_virtual/bin/activate

You shall notice that in the terminal that every line starts with my_venv which indicated that currently the environment known as my_venv has been activated.

Once done, you can now install or uninstall anything, this shall only affect within that specific environment and nowhere else.

Install packages

Because of dependency support you can install packages in venv and it will pull the pip files automatically too with the below command:

[email protected]ide:~#pipenv install 

Clear Existing Environments

Now at times what can be done is that you don’t need to completely delete it you may want to clear the packages that were installed previously. If this is that case then you can clear venv simply. For this:

[email protected]:~#python3 –m –clear path/to/paul-virtual

Update Python version inside venv environment

If you intend to use the existing python version then simple provide with “—upgrade” flag:

[email protected]:~#python3 –m venv /path/to/paul_virtual –upgrade

ALSO SEE: Top 16 PyCharm Keyboard Shortcuts for Windows and Pac (PDF Download).

Easily Create a Virtual Environment

To develop a python library or application the first and foremost thing to do is develop a virtual environment.

[email protected]:~# python3 –m venv paul_virtual

This shall create one which is known as “paul_virtual” and is placed under the current directory. When you have an inspection of the content of the directory created you shall see similar results to the ones mentioned below:

[email protected]:~# ls paul_virtual
bin/ doc/ etc/ images/ include/ lib/ pyvenv.cfg share

Now, if you intend to make a virtual environment under a particular directory then all you need to do is simply include it along with venv name like:

[email protected]:~# Python3 –m venv path/ussr/venv/paul_virtual

Deactivating your Virtual Environment

If you have finished with developing your package and are in need to work with another one then you can deactivate it by running “deactivate” in the command line:

[email protected]:~# deactivate

You shall see that the virtual environment name has disappeared which means it is not active.

Bonus: Deleting your environment – you will have to at some point

It is nothing more than a tree directory that is created with certain content automatically. So, to get rid of venv all you need to do is remove the directory from the disk simply.

[email protected]:~#rm –rf /path/paul_virtual

Conclusion

Python as you know is a programming language that can be used in almost any scenario. Our list of best Python venv commands are specifically helpful for computer programmers, engineers and students who may have multiple projects to work on at the same time. Having them in a different and their own directory and sandbox is crucial so you do not get confused and override other files. Further information can be found on the official documentation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here