Getting started with Python

Python is an interpreted, object-oriented, and high-level computer programming language with very powerful features that's easy to learn because of its simple syntax. For this project, we can easily write an interface between Twitter and Arduino using the Python script.

Installing Python on Windows

The following steps will explain how to install Python on a Windows computer:

  1. Visit https://www.python.org/.
  2. Click on Downloads | Windows.
    Installing Python on Windows

    The Python home page

  3. Then, you will navigate to the Python Releases for Windows web page:
    Installing Python on Windows

    The Python download page

  4. Python can be downloaded from two development branches: legacy and present. The legacy releases are labeled as 2.x.x, and present releases are labeled as 3.x.x. (For reference, the major difference of 2.7.x and 3.0 can be found at http://learntocodewith.me/programming/python/python-2-vs-python-3/). Click on the latest (see the date) Windows x86-64-executable installer to download the executable installer setup file to your local drive under Python 3.x.x.
  5. Alternately, you can download a web-based installer or embedded ZIP file to install Python on your computer.
  6. Browse the default Downloads folder in your computer and find the downloaded setup file. (My default downloads folder is C:Downloads).
  7. Double-click on the executable file to start the setup:
    Installing Python on Windows

    The Python setup

  8. Click on the Run button if prompted as a security warning:
    Installing Python on Windows

    Security warning

  9. The Python setup wizard starts:
    Installing Python on Windows

    The Python setup wizard—start screen

  10. Optionally, you can check Add Python 3.5 to PATH, or later, you can add it using Windows system properties. Click on the Customize installation section. The Optional Features dialog box will appear:
    Installing Python on Windows

    The Python setup wizard—Optional Features

  11. Click on the Next button to proceed. The Advanced Options dialog box will appear. Keep the selected options as default.
    Installing Python on Windows

    The Python setup wizard—Advanced Options

  12. The default installation path can be found under Customize install location. If you like, you can change the installation location by clicking on the Browse button and selecting a new location in your computer's local drive.
  13. Finally, click on the Install button.
  14. If prompted for User Access Control, click on OK. The Setup Progress screen will be displayed on the wizard:
    Installing Python on Windows

    Python setup installation progress

  15. If the setup is successful, you will see the following dialog box. Click on the Close button to close the dialog box:
    Installing Python on Windows

    The Python setup is successful

Setting environment variables for Python

If you have already set to Add Python 3.5 to PATH for writing the environment variables during the Python setup installation process, ignore this section. If not, then follow these steps to set environment variables for Python.

  1. Open the Windows Control Panel and click on System. Then, click on Advanced system settings.
  2. The System Properties dialog box will appear. Click on the Advanced tab. Then, click on the Environment Variables… button:
    Setting environment variables for Python

    The System Properties dialog box

  3. The Environment Variables dialog box will appear. Click on the New… button under user variables:
    Setting environment variables for Python

    The Environment Variables dialog box

  4. The New User Variable dialog box appears:
    Setting environment variables for Python

    The New User Variable dialog box

  5. Type the following for the respective textboxes:
    • Variable name: PATH
    • Variable Value: C:UsersPradeekaAppDataLocalProgramsPythonPython35;C:UsersPradeekaAppDataLocalProgramsPythonPython35Libsite-packages;C:UsersPradeekaAppDataLocalProgramsPythonPython35Scripts;

    Modify the preceding paths according to your Python installation location:

    Setting environment variables for Python

    The New User Variable dialog box

  6. Click on the OK button three times to close all the dialog boxes.
  7. Open Windows Command Prompt and type python, and then press the Enter key. The Python Command Prompt will start. The prompt begins with >>> (three greater than marks):
    Setting environment variables for Python

    Python Command Prompt

This ensures that the Python environment variables are successfully added to Windows. From now, you can execute Python commands from the Windows command prompt. Press Ctrl + C to return the default (Windows) command prompt.

Installing the setuptools utility on Python

The setuptools utility lets you download, build, install, upgrade, and uninstall Python packages easily. To add the setuptools utility to your Python environment, follow the next steps. At the time of writing this book, the setuptools utility was in version 18.0.1.

  1. Visit the setuptools download page at https://pypi.python.org/pypi/setuptools.
  2. Download the ez_setup.py script by clicking on the link (https://bootstrap.pypa.io/ez_setup.py):
    Installing the setuptools utility on Python

    The setuptools download page

  3. The script opens in the browser's window itself, rather than downloading as a file. Therefore, press Ctrl + A to select all the code and paste it on a new Notepad file:
    Installing the setuptools utility on Python
  4. Next, save the file as ez_setup.py in your local drive.
    Installing the setuptools utility on Python
  5. Open Windows Command Prompt and navigate to the location of the ez_setup.py file using the cd command. We assume that the drive is labeled as the letter D:, and the folder name is ez_setup:
    C:>D:
    D:>CD ez_setup
    
    Installing the setuptools utility on Python
  6. Type python ez_setup.py and press the Enter key to run the Python script:
    Installing the setuptools utility on Python

This installs the easysetup utility package on your Python environment:

Installing the setuptools utility on Python

Installing the pip utility on Python

The pip utility package can be used to improve the functionality of setuptools. The pip utility package can be downloaded from https://pypi.python.org/pypi/pip. You can now directly install the pip utility package by typing the following command into Windows Command Prompt:

C:> easy_install pip

However, you can ignore this section if you have selected pip, under Optional Features, during the Python installation.

Installing the pip utility on Python

Opening the Python interpreter

Follow these steps to open the Python interpreter:

  1. Open Command Prompt and type the following:
    C:> Python
    
  2. This command will load the Python interpreter:
    Opening the Python interpreter

To exit from the Python Interpreter, simply type exit() and hit the Enter key.

Installing the Tweepy library

The Tweepy library provides an interface for the Twitter API. The source code can be found at https://github.com/tweepy/tweepy. You do not have to download the Tweepy library to your computer. The pip install command will automatically download the library and install it on your computer.

Follow these steps to install the Python-Twitter library on your Python installation:

  1. Open the Windows command prompt and type:
    C:>pip install tweepy
    
  2. This begins the installation of the Tweepy library on Python:
    Installing the Tweepy library

Installing pySerial

To access the serial port in the Python environment, we have to first install the pySerial library on Python:

  1. Open the Windows Command Prompt and type the following:
    C:>pip install pyserial
    
    Installing pySerial
  2. After installing the pySerial library, type the following command to list the available COM ports in your computer:
    C:/> python -m serial.tools.list_ports
    
    Installing pySerial
..................Content has been hidden....................

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