Python and PluralSight
The Python Developers Toolkit.
Why have a Python Toolkit
- Python has defacto standatds
- Python is an eco systeem and not just a language
Dependancy Management - [PIP]
How to import a package for use
import PACKAGE
Package Locations
import sys sys.path
- Lists the locations and order searched for packages
- First item is
./
site-packages
location is 3rd party and system wide package location
Python Packaging Authority
- PPA advises best current method for package management because the ecosystem is a disaster.
- Use
setuptools
- Use
PIP
Getting PIP
- >= Python 3.4
- Includes PIP with the standard install
- < Python 3.4
- https://www.pip-installer.org
Using PIP
pip install <PACKAGE> pip install <PACKAGE>==1.0 pip install '<PACKAGE>>=1.0'
pip list pip show <PACKAGE>
# Requests are sent to the 'Cheese Shop' https://pypi.python.org/ pip search <QUERY>
pip freeze pip freeze > requirements.txt pip install -r requirements.txt
Environmnet Isolation - [virtualenv]
- Don't install global packages.
- Isolate project package dependancies
- Keep environments together and separate from projects.
- Common location is a
.virtualenv/
directory
pip install virtualenv
.virtualenv/> virtualenv <PROJECT_NAME>
.virtualenv/PROJECT_NAME/bin/activate
# Command injected into path deactive
Helper virtualenvwrapper
pip install virtualenvwrapper
pip install virtualenvwrapper-win
workon workon <PROJECT_NAME>
# Create new project mkproject <PROJECT_NAME> # Fails in windows. # Env for current location mkvirtualenv # Assign environmnet for linux/mac setvirtualenv <PROJECT_NAME> # Assign environmnet for win setprojectdir <PROJECT_NAME> # Create tmp environment (somewhere to test pip installs) mktmpenv
Code Quality - [pylint]
- PEP
- Python Enhancement Proposal
- PEP8
- Style Guide
- Includes Layout, whitespace, comments, naming…
- There is a full PEP for docstring contents.
Pylint :
- is a static code checker.
- in pep8 compliant
- includes error detection
- is configurable
- intergrates with ides.
Running
- GUI (
py
) doesn't work in windows. - init.py">Indicates a python folder
- Pylint only searches python folder for called modules/packages
pylint <PACKAGE/MODULE>
Configuration
pylint --generate-rcfile > pylintrc
Disable Syntax
# pylint: disable=unused-arguments
Debugging - [Python Debugger]
- PDB
- The python debugger.
import pdb pdb.set_trace()
l - location n - next, step over s - step into w - where in stacktrace c - continue b[(file:lineno|function)] - break, add breakpoint h (x) - help on (x) r - return out of this level
Code Documentation - [Sphinx]
PEP 257 :: Docstring semantics and conventions.
- dostring
- first statement of a module, class, function
- maps to the ~__doc__` variable
- start/end with ~'''~
- They are full punctuated senteces.
- include return type, since only the variable name is returned automatically
Sphinx :: The Python documentation Generator
- is the defacto standard
- used by the core python team
- input is reStructured text
- allows output to multiple formats; pdf, html…
- Usage
pip install sphinx
/docs/> sphinx-quickstart
/docs/> edit ./conf.py # theme :: haiku # autodoc :: True
/docs/> make html
- reStructureText
- reStructuredText
- just a text format
- *.rst
- file extenstion
- Sphinx flavor docs
- https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html
- autodoc
- Allows mapping to docstrings in code
- https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#module-sphinx.ext.autodoc
Packaging and Distribution
setuptools :: ./setup.py
from setuptools import setup setup ( ... ) __author__ = 'sre'
Setup function
- doc
- this modules docstring
- include_package_data=True
- Uses MANIFEST.in for assets
- install_requries
- required packages for execution
Building Package
python setup.py sdist
- sdist
- source distribution, basically a zip
- bdist_wheel
- Binary wheel distrubution, (to include c)
- wheel
- python packaging standard
- requirement
pip install wheel
- (no term)
- becoming defacto standard
Deploying Package Locally
pip install <FILE_PATH>
Deploying Package Publically
python setup.py register
python setup.py sdist upload