venv

Posted under » Python » Django on 22 April 2021

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

Before installing django or streamlit, it is best that you install a virtual environment first. Or it could be pipenv, poetry, virtualenv, conda or venv. In this tutorial, we use venv.

To install venv.

$ sudo apt install python3-venv

If you want an older version, assuming your OS python version is 3.10

$ sudo apt install python3.8-venv

Make a new venv like so

$ mkdir venv
$ cd venv
$ python3 -m venv studentals

If older

$ python3.8 -m venv studentals

You need to activate it. You can deactivate it too.

$ source studentals/bin/activate
(student_als) hanafi@anoneh:~/www/student$

You’ll now notice that the name of our virtual environment "student_als" is preceding our command prompt. This indicates we are now working within our activated virtual environment. When working with Django, I always work in a venv environment but I am too lazy to put (student_als) hanafi@anoneh:~/www/student$ so I just put as $ .

Now install Django or MySQLclient or whatever python libraries using pip.

To get out of the environment,

(student_als) $ deactivate
$

It would be a good idea to backup your venv directory just in case it became corrupted. You can also "branch" a venv.

Alternatively, to run a program in this environment, source the bin/activate script in a shell.

$ bash
$ export PYTHONNOUSERSITE=1
$ . ~/venv/studentals/bin/activate
$ ./my_python_program

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data