Go back toDeveloper Overview Home

Python Overview

Python is one of the most accessible and powerful ways to program thegroovEPIC 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 managerpip要从Python软件包索引(PYPI)安装更多外部软件包,以满足您的需求。

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 usingpythonpython3命令区分它们 - 但是拥有软件包管理器之间存在冲突pipinstalled for both versions, since both want to control thepip命令关键字。对于简单应用,这不是问题,但是对于依靠额外包装的应用程序,将系统保持在Python 2 2是理想的选择或者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.pyto launch scripts using Python 3.4

  4. python script.py使用默认的Python 2.7启动脚本。

For managing external packages using thepippackage manager, please read thefollowing sections.

Top

Python OptoMMP Library

To makeOptoMMP Pythonimplementation 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 thePython Package Index (PyPI).

To install the package use the instructions below to getpipfor your version of Python, then runsudo pip install optommp. For more details check outthis forum post.

Top

Python REST Requests

To makeREST APIrequests you will first need to install thePython requests package,然后遵循以下步骤:

  1. Import the requests library at the top of your script.
    导入请求

  2. 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)

  3. Create JSON object for the request header to hold the API key information.
    head = { 'apiKey' : '3KbLb7yZRxnTBP49nEmobDkrpmPkFoBo',
    'Content-Type' : 'application/json' }

  4. Set the URL for the request based on thespecific APIthat 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'

  5. Make the HTTPSgetrequest using the url and header created above, with request verification disabled since the API key will be used instead.
    响应= 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:
    if response.status_code == 200:
    print(response.text)

See theSSH demo filesfor more examples of this API in use.

Top

Python Requests Package

To makeRESTfulrequests with Python you need to install therequestslibrary manually using the Python package manager,pip. When you installpipthere will be a suggestion to update it —do not update pip, the newest version is incompatible and the current version works fine. To usepipyou will first need to install the appropriate package for your Python interpreter usingapt-get.

For Python 2.7

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

    sudo apt-get update

  2. Next, use your package tool to installpip. DoNOTupgrade 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.

    导入请求

For Python 3.4

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

    sudo apt-get update

  2. Next, use your package tool to installpip. DoNOTupgrade 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.

    导入请求

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 uninstallpython-pipfor Python 2.7, for example to move to pip for Python 3.4:

    sudo apt-get remove python-pip

  3. To uninstallpython3-pipfor Python 3.4, for example to move to pip for Python 2.7:

    sudo apt-get remove python3-pip

Top