DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

The use of Python 3 has changed dramatically as support
for the 2.x version ended in January 2020. The 3.x
version came out in 2008 and is the one that you should
be using today. Of course, this version issue is still a
problem even within the 3.x train of Python and the
corresponding modules you may want to use. To address
this compatibility conundrum across different versions
and modules, Python virtual environments have been
created. Such an environment allows you to install a
specific version of Python and packages to a separate
directory structure. This way, you can ensure that the
right modules are loaded, and your applications don’t
break when you upgrade your base Python installation.
As of Python 3.3, there is native support for these virtual
environments built into the Python distribution. You can
code in Python without using virtual environments, but
the minute you update modules or Python itself, you run
the risk of breaking your apps. A virtual environment
allows you to lock in the components and modules you
use for your app into a single “package,” which is a good
practice for building Python apps.


To use virtual environments, you launch Python 3 with
the -m argument to run the venv module. You need to
supply a name for your virtual environment, which will
also become the directory name that will include all the
parts of your virtual environment. Next, you need to
activate the virtual environment by using the source
command in Linux or Mac, as shown in this example. On
Windows, you will need to run the activate batch file.


Click here to view code image


# MacOS or Linux
python3 -m venv myvenv
source myvenv/bin/activate

# Windows
Free download pdf