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:

Top

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:

  1. sudo apt-get update

  2. sudo apt-get install python3

  3. python3 script.py to launch scripts using Python 3.4

  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.

Top

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.

Top

Python REST Requests

To make REST API requests you will first need to install the Python requests package, and then follow these steps:

  1. Import the requests library at the top of your script. python import requests
  2. Ignore the insecure https request warning, since we will authenticate with an API key rather than certificates. python from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  3. Create JSON object for the request header to hold the API key information. python head = { 'apiKey' : '3KbLb7yZRxnTBP49nEmobDkrpmPkFoBo', 'Content-Type' : 'application/json' }
  4. 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. python url = 'https://opto-01-02-03/manage/api/v1/io/local/modules/2/analog/values'
  5. Make the HTTPS get request using the url and header created above, with request verification disabled since the API key will be used instead. python response = requests.get(url, headers=head, verify=False)
  6. Check the response code, where code 200 means the request was successful, then print the response text: python if response.status_code == 200: print(response.text)

See the SSH demo files for more examples of this API in use.

Top

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

  1. First, make sure your package tool is up-to-date with your package repositories. Do NOT apt-get upgrade!

    sudo apt-get update

  2. Next, use your package tool to install pip. Do NOT upgrade pip, regardless of warnings.

    sudo apt-get install python-pip

  3. Use the Python package manager to install the requests library.

    sudo pip install requests

  4. In your Python scripts make sure you import the library once it has been installed.

    import requests

For Python 3.4

  1. First, make sure your package tool is up-to-date with your package repositories. Do NOT apt-get upgrade!

    sudo apt-get update

  2. Next, use your package tool to install pip. Do NOT upgrade pip, regardless of warnings.

    sudo apt-get install python3-pip

  3. Use the Python package manager to install the requests library.

    sudo pip install requests

  4. In your Python scripts make sure you import the library once it has been installed.

    import requests

Checking pip Version and Uninstalling

  1. To check the version of pip you have installed, and to see which version of Python it is tied to:

    pip -V

  2. To uninstall python-pip for Python 2.7, for example to move to pip for Python 3.4:

    sudo apt-get remove python-pip

  3. To uninstall python3-pip for Python 3.4, for example to move to pip for Python 2.7:

    sudo apt-get remove python3-pip

Top