Skip to content

Python

General

Style

Style Guide

Tools

  • Flake8
    $ flake8 path/to/code/to/check.py
    # or
    $ flake8 path/to/code/
    

Poetry

Initialization

$ poetry new my-repo
This will create a project ./my-repo/. For existing project, do:
$ cd pre-existing-project
$ poetry init

Dependencies

$ poetry add numpy

Virtual ENV

$ poetry run python your_script.py
$ poetry run pytest

# or
$ poetry shell

Publish

  • Config PyPI token
  • Publish
    $ poetry build
    $ poetry publish
    
  • Change version number
    # Print version number
    $ poetry version
    $ poetry version -s
    
    # Bump version number
    $ poetry version patch
    $ poetry version minor
    $ poetry version major
    $ poetry version [patch | minor | major | prepatch | preminor | premajor | prerelease]
    

Conda

Installation

Install Package

$ conda install xxx
$ conda install -c conda-forge xxx

Useful Tools

$ conda install -c conda-forge tmux

Post-installation

$ conda config --set auto_activate_base false
# It creates ~/.condarc with following content:
    auto_activate_base: false

Jupyter notebook

$ conda install ipykernel
$ python -m ipykernel install --user --name lal --display-name "python-lal"
# It modifies ~/Library/Jupyter/kernels/  or  ~/.local/share/jupyter/kernels/

Conda ENV

$ conda create -n py37 python=3.7
$ conda activate py37
$ conda deactivate 
$ conda env list
$ conda env remove -n py37

HTTP server

Star a http server (e.g., for file sharing)

$ python3 -m http.server

Virtual Environment

  • virtualenv
    $ virtualenv <myenv>
    $ source myenv/bin/activate
    $ deactivate
    
  • pip
    $ pip freeze > requirements.txt
    $ pip install -r requirements.txt