Python Basics

Just some notes in my attempts to become more adept at python.

Python Package Basics

The waters here are a bit murky. What follows is my attempt to demurk them somewhat.

distutils

The first and basic package here is distutils. It comes with python. This is the basic standard package you use when you want to describe a particular package that you have written and you want to distribute to other people. At its core, it provides a way for you to describe your package so that other people may install it on their systems.

You just call

python setup.py install

and you're good to go.

setuptools

Next we have setuptools, which provides the easy_install command.

Basically setuptools provides an extra layer on top of the plain vanilla distutils. You can think of distutils as providing metadata about the structure of your package, while setuptools provides extra information about dependencies and other things.

setuptools allows you to specify dependencies, and has a test hook which you can configure with the test_suite, tests_require, etc. You can then run tests like this:

python setup.py test

Note that this doesn't actually install any testing software, if any was was specified. The whole point is that it runs the tests outside of a python installation.

You can also declare "optional" fetures. All the extra metadata is used when you run easy_install, or when you run the setup.py script.

There is a package called Distribute which is a fork of setuptools.

pip

Setuptools seems to be supplanted by pip. With pip, you just install a package like this on the command line:

pip install <package>

Or if you want a particular version:

pip install <package>==<version>

The setup.py file is read. It will download the package and its dependencies from the Python Package Index, though you can change that with the dependency_link option in the setup file.

See these guides:

Virtualenv

Good info on the difference between the different python virtual env tools:

https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe

For python 2:

Use this to isolate different python environments.

cd <base directory for your virtualenvs>
virtualenv --system-site-packages <virtualenv directory>

Or if you can choose not to give access to the global packages:

virtualenv --no-site-packages <virtualenv directory>

You switch into (activate) a virtualenv by running this:

source <path_to_virtualenv>/bin/activate

Once you set everything up you can take a snapshot of the packages you have install like this:

pip freeze > requirements.txt

But do this after you have switched into/activated the virtual env.

This requirements file can be used with a new virtualenv to recreate the setup you had before:

pip install -r requirements.txt

You can also use virtualenvwrapper to manage difference virtualenvs. It give you the ability to switch between virtualenvs, create them, delete them, etc.

Python Checking

Several tools exist for checking python source code.

I'm thinking that pylint and pep8 might be the way to go here...

Different Approaches

Modules

Basically, a file ending in .py defines a module. A package is a collection of modules hanging off a common top-level directory

Python Web Stuff

This is just a page for jotting down notes on web related technologies in python. There's a boat load of web frameworks in Python: "X is an open source web framework written in Python" is something you see a lot of on Wikipedia.

Almost all of them provide some support for Web Server Gateway Interface (WSGI), a python centric interface between applications and web servers. The equivalent in Ruby is Rack.

Some python frameworks are fairly heavy weight. By that I mean that they include built-in support for DBs, ORMs, templates, etc. For example:

Some are more lightweight:

Python Argument Passing

I always need reminding about this one. From the programming "Python Book":

PythonBasics (last edited 2018-09-03 16:55:20 by DesmondRivet)