Python

From Klenwell Wiki
Jump to navigation Jump to search

Overview

This is where I collect best practices related to Python.

Setup

Python 2 or Python 3?

Python 3.[1]

Pyenv

To manage different versions of Python on your machine, use pyenv. To install see:

Once it's installed, install the version of Python you want:

$ pyenv -v
pyenv 1.2.6

$ pyenv install -l
Available versions:
  2.1.3
  2.2.3
...
  3.8-dev
  3.8.1
  3.9-dev
...
  stackless-3.4.7
  stackless-3.5.4

# Install latest stable version of Python 3. At this time: 3.8.1
$ pyenv install 3.8.1
$ pyenv global 3.8.1
$ pyenv global
3.8.1
$ python -V
3.8.1

# Set up new virtualenv
pyenv virtualenv 3.8.1 my-virtual-env
pyenv local my-virtual-env

# Force pyenv to load (have needed to do this at times since installing conda)
pyenv init

Pip

To manage dependencies, use pip. (It is installed with pyenv.)

Project Structure

For guidance on project structure, see the following:

Style Guide

Unless otherwise noted, follow PEP 8.

  • Use spaces not tabs.
  • Indent 4 spaces.

Code Analysis

Use of a code analyzer, or linter, is strongly encouraged. We use flake8:

Notes

  1. Unless it is an existing project or absolutely depends on a package that is only compatible with Python 2. All new projects should target latest version of Python 3.