Python
Jump to navigation
Jump to search
Contents
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:
- https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application
- Especially this answer: https://stackoverflow.com/a/3419951/9381758
- http://docs.python-guide.org/en/latest/writing/structure/
- https://github.com/kennethreitz/setup.py
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
- ↑ 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.