Create virtual environments in Python to develop apps in Django or Flask

In this post we are going to talk about virtual spaces in Python; specifically our case of interest which would be to create applications in Django or Flask.

Virtual spaces are a tool used to create a Python environment isolated from the rest of the Operating System and in this way we can develop our Python applications with simple versions of a specific framework, Python or its dependencies.

Specifically it is used when developing software with Python; It is very common to want to use several versions of the same package in different projects; and to avoid installing and uninstalling (since we cannot install a package twice in our OS or venv), we can create an environment with the specific objective of isolating these multiple versions of this or various packages; for this reason, in this space you will find only the packages that you are going to use to develop your applications in Python, either native or with a framework such as Django or Flask.

In short, with virtual environments we can install a specific version of that package that we need for a particular project or projects, as well as the rest of the dependencies that a project has.

Why use virtual spaces in practice?

The next thing you may wonder is what are we going to use virtual spaces or venv for in Python; venv are used to sandbox our OS, with this, we can configure which version of Python we want to use (in case we have multiple Python installations on your machine) or install specific packages for a project; and this has been one of the most interesting points of using Python's virtual spaces.

How to install a virtual environment?

We can get hold of the tool that allows us to create venv or virtual environments on our computer; as you can see, it is only a package that we can do using the pip that reminds us that we already have it in recent versions of Python:

pip3 install virtualenv

Creating a venv

Now with this we can create virtual spaces; for that the following command:

virtualenv <myproject>

Where <myproject> is the name of your virtual space; However; With the previous command, a folder was created with the name of the venv that you defined in the previous command, and that folder is your virtual space ready to use.

Now, to use the previous virtual space, we simply have to move through our CMD or terminal into the folder called Script; which is where our Script is located to be able to manage the virtual space through the binary files contained in it; to activate or deactivate the virtual environment.

Activate / Deactivate virtual environment

Being in the location mentioned above, you will see that we have several files to work with our environment or virtual space in Python, some called activate / deactivate that allow us to activate or deactivate the virtual space.

The first thing we would like to do is activate our virtual environment:

activate

Or if we want to disable the virtual environment:

deactivate

This would be our initial state before executing the activate command inside the Script folder:

C:\Users\andre\OneDrive\Escritorio\myproject\Scripts>

Now, we are going to activate it's virtual space with the command:

activate

And now our terminal looks like this:

venv activado en terminal o CMD

And with this we can do everything we want; for example, we can create a specific project in Flask or Django or just the latest one on the market

pip install django

Use case: Get all the packages of a venv or OS

For example, suppose you have project-specific versions of Flask, Django, or something else; In the case of the project that we followed in the previous Django course, it would be the following:

appdirs==1.4.3
asgiref==3.2.4
astroid==2.3.3
certifi==2020.4.5.1
cffi==1.14.0
chardet==3.0.4
colorama==0.4.3
cryptography==2.9
defusedxml==0.6.0
diff-match-patch==20181111
distlib==0.3.0
Django==3.0.4
django-admin-interface==0.12.2
django-bootstrap-form==3.4
django-colorfield==0.3.0
django-cors-headers==3.2.1
django-debug-toolbar==2.2
django-filter==2.2.0
django-flat-responsive==2.0
django-flat-theme==1.1.4
django-grappelli==2.14.1
django-import-export==2.0.2
django-material==1.6.3
django-sql-explorer==1.1.3
djangorestframework==3.11.0
easy-thumbnails==2.7
et-xmlfile==1.0.1
filelock==3.0.12
idna==2.9
isort==4.3.21
jdcal==1.4.1
lazy-object-proxy==1.4.3
MarkupPy==1.14
mccabe==0.6.1
odfpy==1.4.1
openpyxl==3.0.3
paypalrestsdk==1.13.1
Pillow==7.0.0
pycparser==2.20
pylint==2.4.4
PyMySQL==0.9.3
pyOpenSSL==19.1.0
pytz==2019.3
PyYAML==5.3.1
requests==2.23.0
rjsmin==1.1.0
six==1.14.0
sqlparse==0.3.1
tablib==1.1.0
unicodecsv==0.14.1
urllib3==1.25.9
virtualenv==20.0.17
wrapt==1.11.2
xlrd==1.2.0
xlwt==1.3.0

You can get all the packages installed in your OS or virtual environment with the command:

pip freeze

And you can save it to a file so that you can specifically specify which are the specific versions of your project:

pip freeze > requirements.txt

Which you can then easily install by:

pip install -r requirements.txt

Therefore, you can easily create an environment for a particular project or just for the project you are carrying out using the virtual environments; how do you see the huge advantage we have is that we can install specific versions of our packages.

At this point we are ready and we can start developing our applications in Flask or start developing our applications in Django.

- Andrés Cruz

En español
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.