Go back to Developer Overview Home
Python Overview
Python is one of the most accessible and powerful ways to program the groov EPIC via the command line, with a Python 2.7 interpreter available out-of-the-box and the option to install Python 3.4. As well as having a large and diverse standard library you can use the package manager pip to install more external packages from the Python Package Index (PyPI) to meet your needs.
Additional resources:
- Python Software Foundation
- W3Schools Python Tutorial
- Codecademy Python Tutorial
- Python Package Index (PyPI)
Python 2.7 and Python 3.4
The EPIC can run both Python 2 and Python 3 interpreters, and can have both installed at the same time using python
and python3
commands to differentiate them – however there is conflict between having the package manager pip installed for both versions, since both want to control the pip
command keyword. For simple applications this isn’t a problem, but for applications that rely on extra packages it would be ideal to keep your system at either Python 2 or Python 3 exclusively and avoid swapping between them.
To install Python 3:
-
sudo apt-get update
-
sudo apt-get install python3
-
python3 script.py
to launch scripts using Python 3.4 -
python script.py
to launch scripts using the default Python 2.7.
For managing external packages using the pip package manager, please read the following sections.
Python OptoMMP Library
To make OptoMMP Python implementation more straightforward and easier to put into use there is a Python package available that simplifies commands to single-line function calls with just an object for the EPIC processor and and a line to import the library which is managed by the Python Package Index (PyPI).
To install the package use the instructions below to get pip for your version of Python, then run sudo pip install optommp
. For more details check out this forum post.
Python REST Requests
To make REST API requests you will first need to install the Python requests package, and then follow these steps:
-
Import the requests library at the top of your script.
import requests
-
Ignore the insecure https request warning, since we will authenticate with an API key rather than certificates.
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) -
Create JSON object for the request header to hold the API key information.
head = { 'apiKey' : '3KbLb7yZRxnTBP49nEmobDkrpmPkFoBo',
'Content-Type' : 'application/json' } -
Set the URL for the request based on the specific API that you are using, in this example we are using the Manage API to get analog values from module 2.
url = 'https://opto-01-02-03/manage/api/v1/io/local/modules/2/analog/values'
-
Make the HTTPS get request using the url and header created above, with request verification disabled since the API key will be used instead.
response = requests.get(url, headers=head, verify=False)
-
Check the response code, where code 200 means the request was successful, then print the response text:
if response.status_code == 200:
print(response.text)
See the SSH demo files for more examples of this API in use.
Python Requests Package
To make RESTful requests with Python you need to install the requests
library manually using the Python package manager, pip. When you install pip there will be a suggestion to update it — do not update pip, the newest version is incompatible and the current version works fine. To use pip you will first need to install the appropriate package for your Python interpreter using apt-get.
For Python 2.7
-
First, make sure your package tool is up-to-date with your package repositories. Do NOT apt-get upgrade!
sudo apt-get update
-
Next, use your package tool to install pip. Do NOT upgrade pip, regardless of warnings.
sudo apt-get install python-pip
-
Use the Python package manager to install the requests library, getting version 2.7 compatible with Python 2.7.
sudo pip install requests==2.7
-
In your Python scripts make sure you import the library once it has been installed.
import requests
For Python 3.4
-
First, make sure your package tool is up-to-date with your package repositories. Do NOT apt-get upgrade!
sudo apt-get update
-
Next, use your package tool to install pip. Do NOT upgrade pip, regardless of warnings.
sudo apt-get install python3-pip
-
Use the Python package manager to install the requests library.
sudo pip install requests
-
In your Python scripts make sure you import the library once it has been installed.
import requests
Checking pip Version and Uninstalling
-
To check the version of pip you have installed, and to see which version of Python it is tied to:
pip -V
-
To uninstall python-pip for Python 2.7, for example to move to pip for Python 3.4:
sudo apt-get remove python-pip
-
To uninstall python3-pip for Python 3.4, for example to move to pip for Python 2.7:
sudo apt-get remove python3-pip