Python for Automation of IoT Tasks
Get set up with a Python interpreter so that you can begin to automate many administrative tasks. There are many other possible choices of languages for automation of these ThingPark and IoT administration tasks, and many of the tips given here will be equally applicable to these other languages.
Install Python
Python is a cross-platform scripting language and is available for many different platforms. Mac and Windows download-links are given below:
and GNU/Linux systems will probably have at least one version of Python installed already. There are many resources available to help you with correctly installing and setting-up Python, so that information will not be presented here. The primary purpose of this page is to provide links and just enough information to give an overview of what you will likely need to know, or refresh one’s memory, for automating ThingPark DX API tasks using Python scripts.
PIP for Managing Python Packages
PIP (“Pip Installs Packages”) is the default Python package manager (from Python version 3.4 onwards) and it is a convenient utility for installing additional Python packages, and managing Python project dependencies. Installing new Python packages simply requires that you issue a command of the form:
Selecting your Python Version
If you have multiple versions of Python installed, you can use pip3
to force Python 3 packages to be installed.
And to remove that package, you again use the pip
(or pip3
) command:
where <package>
is a placeholder to represent the name of a particular package (and the actual package name will not contain angle-brackets); e.g., you may want to (un)install the virtualenv
utility to manage Python virtual environments – and this is simply one of the following commands:
Virtual Environments
Virtual environments ensure that dependencies can be installed without conflicting with, or breaking, other installed packages. There is a lot of excellent guides available for this, including:
The following subsection gives a quick overview of how to setup and use virtualenv
– which may be of more use if one has used similar tools before, or if it has simply been a while.
Setting up Virual Environments
Install a suitable version of Python (for example v2.7 or v3.6.2), along with pip
(or, pip3
), to install Python packages, and then:
where venv
is where you would like to store the Python virtual environment modules.
Setup Tips
On GNU/Linux systems, virtualenv
will typically be installed to a user’s .local/bin/
subdirectory, and this path should be added to their PATH
environment variable; e.g., by adding it to their /home/<user>/.bashrc
file of their home directory. This may require adding a line that is similar to:
The general “flow” to use these scripts would be:
While inside your virtual environment, the Python interpreter and packages of that environment will be visible and active. The deactivate
command reverts your Python environment back to the system defaults.
Setup Tips
If you are using a tool like Dropbox to share your project/code amongst multiple devices, you can exclude your virtual environment files from being synced across all devices by executing the following command, and with the following output:
and it is best to do this before executing the $ virtualenv venv
command.
Managing Project Dependencies
Existing Python projects/repositories often include a requirements.txt
file, which lists the packages/dependencies for the current project. These packages can be installed by simply issuing the command:
This will fetch all of the specified packages, and with versions that match those of the requirements.txt
file. You can create your own list of dependencies for your scripts by using the following command:
Source Version Control
When we are using the git
source version-control tool, then we like to add the venv
subdirectory to our .gitignore
file; e.g.,
as well as adding the requirements.txt
file to the repository. This allows you to quickly check-out and beginning developing on another machine, which is particularly useful for other developers using your code. They will be able to set up an environment with the exact same versions of all of the Python packages that you used during development (but without having to add those packages to your repository).
Next Steps
We are in the process of documenting some examples of TPW tasks that we hope some subscribers will find useful. Please contact us if you feel that there is a DX API example task that should be useful to other TPW users.