Creating a virtual environment with Python 3.6.x and PEP 405

In the next sections and chapters, we will be writing different pieces of Python code that will subscribe to topics and will also publish messages to topics. As happens whenever we want to isolate an environment that requires additional packages, it is convenient to work with Python virtual environments. Python 3.3 introduced lightweight virtual environments, and they were improved in Python 3.4. We will work with these virtual environments, and therefore, you will need Python 3.4 or greater. You can read more about PEP 405 Python Virtual Environment, which introduced the venv module, here: https://www.python.org/dev/peps/pep-0405.

All the examples for this book were tested on Python 3.6.2 on macOS and Linux. The examples were also tested on the IoT boards mentioned throughout the book and their most popular operating systems. For example, all the examples were tested on Raspbian. Raspbian is based on Debian Linux, and therefore, all the instructions for Linux will work for Raspbian.

If you decide to use the popular virtualenv (https://pypi.python.org/pypi/virtualenv) third-party virtual environment builder or the virtual environment options provided by your Python IDE, you just have to make sure that you activate your virtual environment with the appropriate mechanism whenever it is necessary to do so, instead of following the step explained to activate the virtual environment generated with the venv module integrated in Python.

Each virtual environment we create with venv is an isolated environment and it will have its own independent set of installed Python packages in its site directories (folders). When we create a virtual environment with venv in Python 3.4 and greater, pip is included in the new virtual environment. In Python 3.3, it was necessary to manually install pip after creating the virtual environment. Note that the instructions provided are compatible with Python 3.4 or greater, including Python 3.6.x. The following commands assume that you have Python 3.5.x or greater installed on Linux, macOS, or Windows.

First, we have to select the target folder or directory for our lightweight virtual environment. The following is the path we will use in the example for Linux and macOS. The target folder for the virtual environment will be the HillarMQTT/01 folder within our home directory. For example, if our home directory in macOS or Linux is /Users/gaston, the virtual environment will be created within /Users/gaston/HillarMQTT/01. You can replace the specified path with your desired path in each command:

~/HillarMQTT/01

The following is the path we will use in the example for Windows. The target folder for the virtual environment will be the HillarMQTT1 folder within our user profile folder. For example, if our user profile folder is C:Usersgaston, the virtual environment will be created within C:UsersgastonHillarMQTT1. You can replace the specified path with your desired path in each command:

%USERPROFILE%HillarMQTT1

In Windows PowerShell, the previous path would be:

$env:userprofileHillarMQTT1

Now, we have to use the -m option followed by the venv module name and the desired path to make Python run this module as a script and create a virtual environment in the specified path. The instructions are different depending on the platform in which we are creating the virtual environment.

Open Terminal in Linux or macOS and execute the following command to create a virtual environment:

python3 -m venv ~/HillarMQTT/01

In Windows, in Command Prompt, execute the following command to create a virtual environment:

python -m venv %USERPROFILE%HillarMQTT1

If you want to work with Windows PowerShell, execute the following command to create a virtual environment:

python -m venv $env:userprofileHillarMQTT1

None of the previous commands produce any output. The script created the specified target folder and installed pip by invoking ensurepip because we didn't specify the --without-pip option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.20