Python Developer’s Toolkit (using Standard Tools)

Problem Statement:

To be able to code WordPress, I need to understand Python

  • Python 2.7 deprecated in Jan 1, 2020, (drop support Jan 1, 2021)

 

Solution:

Learn from online classes that have a focus on command line

 

Topics covered:

  • Installation
  • Managing Python packages with pip (dependency management)
  • VirtualEnv and VirtualEnvWrapper: isolated Python environments
  • Checking code quality with pylint
  • Python debugger
  • Generating documentation with Sphinx
  • Packaging and distributing you project

 

$> dos cmd prompt
>>> python prompt

 

Installation

Go to python download page to get your version.

Run the installer

That easy.

 

Managing Python packages with pip (dependency management)

Watch out – there is a lot of outdated info on the web.  Look here for packaging user guild: python-packaging-user-guide

 

To view packages:
$> python
>>>import sys
>>>sys.path // to view python path

['', '$\Python\Python38-32\python38.zip', '$\Python\Python38-32\DLLs', '$\Programs\Python\Python38-32\lib', '$\Programs\Python\Python38-32', '$\Programs\Python\Python38-32\lib\site-packages']

'' = current working directory
'$\Programs\Python\Python38-32\lib\site-packages' = location for 3rd party packages (debian will be call ‘dist-packages’)

How to create, distribute and install packages?

Python Packaging Authority (PyPA)

Use: setuptools (distutils is discontinued, merged with setuptoosl)

Easy_install is included in setuptools

 

Install Packages:

use: PIP  (from CMD line) for system wide, (not recommended, see next section VirtualEnv)

  • Python 3.4 and later is pre-installed (your good, nothing to do)
  • Pre 3.4 goto http://www.pip-installer.org
    • Download and run git_pip.py
    • Open in Windows to run installer.

 

$>pip install requests
$>python
>>>import requests
>>>import requests==1.0 (for specific version)
>>>import 'requests>2.1' (for specific version, remember the quotes)
>>>import sys
>>>sys.path
>>>CNL+Z enter to exit
$>pip uninstall requests to remove a package (clean up)

 

PIP commands (from command line)

$>pip list (name and version)
$>pip show (location and dependencies)
$>pip search query ( )
$>pip freeze > requirements.txt (export list of currently installed packages to file)
$>pip install -r requirements.txt (install to new environments system what is in the file)
PyPi (cheese shop) Python Package Index http://pypi.python.org
$>pip install flask (pip will also install dependencies: ItsDangerous Jinja2 MarkupSafe Werkzeug)

VirtualEnv and VirtualEnvWrapper: isolated Python environments

VirtualEnv – isolated environment with dependencies not crossing (no system wide packages cross impact

 

$>mkdir .virtualenvs (.virtualenvs is the standard name)
$>cd .virtualenvs
$>python -m venv amaze\venv (amaze is the project name [can be anything you want ie: dev, shadowforce])

/amaze/ (your project files/folders)
/amaze/requirements.txt  (enable rebuild of venv)
/amaze/venv  ( \venv is to segragate the virtual environment from the project to be excluded form source control)

$>amaze\venv\Scripts\activate.bat

(amaze) $>\.virtualenvs>

$> where python

$>\.virtualenvs\amaze\Scripts\python.exe
$>\Programs\Python\Python38-32\python.exe

$> deactivate (to stop using the project environment)

$> rmdir amaze /s (delete permanently)

 

Community & Code Quality

PEP : Python Enhancement Proposal

  • Provide information to the Python community or
  • Describe a new feature for Python

PEP8 : Style Guide for Python Code, and best practices

  • Code layout, readability
  • White-space
  • Comments
  • Naming convention

 

It’s about readability (if you don’t know the rules, it will tell you)

Indentation: 4 spaces per level

Max line length: 79 char

Blank Lines

  • 2 lines between top-level functions and classes
  • 1 line between methods inside a class

Imports

Use of spaces in expressions

Naming

  • Modules: short, lowercase names
  • Classes: Capitalized Naming
  • Functions and methods: lowercase_with_underscores
  • Constants: ALL_CAPS_WITH_UNDERSCORES
  • Non-public names start with underscore.

 

$>pip install pylint

pylint requires __init__.py in the package folder you are checking

$> pylint amaze

$> pylint-gui Need to research why I could not get this to work…

 

Can ignore by typing the error type to a disable line in the code:

# pylint: disable=unused-argument
# pylint: disable=invalid-name

 

$> pylint --generate-rcfile to allow global changes

 

Python Debugger

Add to code:

>>> import pdb
>>> pdb.set_trace()

While in the debugger (lower case):

  • can type commands
    • self._x (will print _x value)
  • L = list, show current line
  • N = next, execute line, move to next
  • S = step, step down into next level
  • W = where, show call stack
  • C = continue, to next break point
  • R = return, step out of this level
  • B = break point, [file:]:lineno
  • H = help, Provides documentation
  • use arrows to recall commands

PyCharm (IDE w/debugger)

 

Documentation

Standard tools for PDF & HTML

PEP257 – Semantics and conventions associated with Python docstrings.

Always surround with “””triple double quotes.””” and end with a period (can be multi line with same indent).

Standard tool “Sphinx” for documentation.

$>amaze\venv\Scripts\activate.bat

$>pip install sphinx

$>mkdir docs

$>cd docs

$>sphinx-quickstart

Answer yes to autodoc, the rest can be the default.

 

modify docs/index.rst to meet your needs

$>make html

Sphinx will tell you the output directory is: _build/html

 

Packaging and Distribution your project

  • setuptools
  • Upload to PiPy
  • Standalone EXE

 

Add file called setup.py with the below content:

from setuptools import setup
setup(name='amaze',
version='0.1',
description='Maze gen',
author='q',
autho_email='non-ya@biz.com',
packages=['amaze', 'amaze.demo'],
)
__author__ = 'Q'

$>python setup.py sdist
Two (2) directories will be created:

  • amaze.egg-info (can be deleted)
  • dist (the package we want to share)
    • will contain: amaze.tar.gz

 

Install in new virtenv with PIP:

$> pip amaze.tar.gz

 

Share with PyPi:

$> python setup.py register

Follow the instructions.

 

Now upload

$> python setup.py sdist upload sdist is source distribution.

Python is moving to bdist_wheel (need to research).

 

Share an execute
Update setup.py by adding:

entry_points={
'console_scripts': [
'amaze_demo=amaze.demo.tkdemo:main',
],
},

 

Py2Exe

 

Conclusion:

This was all about managing packages.  Not sure how much of this I will use on a daily basis, but it is good to know how.

 

Reference:

https://www.python.org/downloads/

https://docs.python.org/3/library/venv.html

https://www.youtube.com/watch?v=APOPm01BVrk

https://www.jetbrains.com/pycharm/

http://sphinx-doc.org/

http://sphinx-doc.org/rest.html#rst-primer

github.com/pypa/sampleproject/ (sample package by PyPA)

http://www.py2exe.org Windows only

http://www.pyinstaller.org/  Multiple platforms