Monday, November 17, 2014

Python for Windows/Linux, Coding Guidelines (PEP8) and PyDev...the Reader's Digest Version

Python for Windows

Download Python

  • https://www.python.org/download/
  • by default, installs to c:\Python## where ## is version number, ex Python34
  • installer updates/adds HKEY_LOCAL_MACHINE\SOFTWARE\PYTHON registry entry

Adding HKEY_CURRENT_USER Registry Entry

Some Python module installers look in the HKEY_CURRENT_USER registry entry for the Python settings so need
to get the info from there:
  • http://choorucode.com/2012/04/28/python-version-not-found-in-registry-error/
    1. open regedit
    2. navigate to Computer->HKEY_LOCAL_MACHINE->SOFTWARE->Python->PythonCore
    3. right click 3.4->Export
    4. save as .reg file on your system, ex. c:\Users\yourname\python3.4.reg
    5. edit .reg file and change all HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER
    6. open cmd box as administrator
    7. cd to where the .reg fiel was saved
    8. install using regedit command
      • c:> regedit python3.4.reg
        • keys should be successfully added to registry
        • verify using that Computer->HKEY_CURRENT_USER->SOFTWARE->Python->PythonCore exists

Python for Linux

Below are some Python libraries that I needed to download:
  • > sudo apt-get update
  • > sudo apt-get upgrade
  • > sudo apt-get install libpq-dev python-dev libjpeg-dev libpng-dev

Python Common

Installing Python Modules

Module install can be done using pip or by double clicking the module if it is an installer.

Python Communities

For Q&A with history, see the Python Forums.

Python Coding Guidelines

Pep8 is the coding convention guideline for Python.  There are different command line tools and IDE tools that can be used to help enforce the guidelines.
  • pep8.py and autopep8.py are 2 modules that can be used
  • PyDev (eclipse plugin) can be configured to use pep8
    • in eclipse, go to Window->Preferences->PyDev->Editor->Code Analysis
    • select pep8.py tab
    • choose the radio button for warning and click Ok
  • http://pep8online.com/
    • browser based python code checker
    • go to the url, paste in your code, see the code guideline violations

PyDev

PyDev is a Python IDE for Eclipse, which may be used for Python, Jython and IronPython development.  It supports code creation, code refactoring, graphical debugging, code analysis and many other features.
Below are some helpful links:
In order to use PyDev, you must have Python (3.4)  and Eclipse (Kepler or Luna) installed.  This page assumes both are already installed.

PyDev Install

PyDev is a Python IDE for Eclipse, which may be used for Python, Jython and IronPython development.  It supports code creation, code refactoring, graphical debugging, code analysis and many other features.
Below are some helpful links:
In order to use PyDev, you must have Python (2.7/3.4)  and Eclipse (Kepler or Luna) installed.  This page assumes both are already installed.
  • install  eclipse pydev plugin
    • open eclipse -> Help -> Eclipse Marketplace...
    • type Python in Find: box and hit enter
    • in PyDev -Python IDE for Eclipse, click Install
      • accept license and install
  • configure Python install location in eclipse
    • Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter
    • Press the New... button and enter Interpreter Name and path to your python.exe
    • click OK,  then click OK again on the System PYTHONPATH dialog box and OK on the Python Interpreters dailog box

Configure PyDev to use pep8

    • in exclise, go to Window->Preferences->PyDev->Editor->Code Analysis
    • select pep8.py tab
    • choose the radio button for warning and click Ok

Unresolved Import Errors

Problem:
Imports cause compile errors even when path to module is appended to the sys path:
import sys
sys.path.append('..')
sys.path.append('../../lib/Core')
sys.path.append('../../lib/Insight')
from SysTestCommon import SysTestCommon
from RemoteLog import python_grep
 
Unresolved import: SysTestCommon
 
settings Found at: TestBidderReqs
from SysTestCommon import SysTestCommon

Solution:
  • make sure any folders with modules you want to import has an __init__.py
    • __init__.py file makes the folder a Python package which makes all modules in the package accessible via import statements
  • right click your pydev project and select Properties
  • select PyDev - PYTHONPATH -> External Libraries
  • select Add source folders (any folder that has an __init__.py) and add any source files you want your project to have access to
  • click OK
  • restart eclipse for change to take effect (File -> Restart)
  • your project code will then be able to import modules from those source folders.

No comments:

Post a Comment