Python - rulebook
Fortunately or not, python is unavoidable. So we need some rules to keep ourselves sane while using it.
XDG and home dir
First, let’s remove the clutter:
- Create
startup.py
file at"$XDG_CONFIG_HOME"/python/
and add the following code:import atexit import os import readline histfile = "" cachedir = os.getenv("XDG_CACHE_HOME") if cachedir == "": cachedir = os.path.expanduser("~") histfile = os.path.join(cachedir, "history") else: histfile = os.path.join(cachedir,"python","history") hist_len = 0 try: readline.read_history_file(histfile) hist_len = readline.get_current_history_length() except FileNotFoundError: open(histfile, 'wb').close() def save(prev_hist_len, histfile): new_hist_len = readline.get_current_history_length() readline.set_history_length(-1) readline.append_history_file(new_hist_len - prev_hist_len, histfile) atexit.register(save, hist_len, histfile)
- Add (or export)
PYTHONSTARTUP
variable, i.e.export PYTHONSTARTUP="$XDG_CONFIG_HOME"/python/startup.py
- Create
history
file at"$XDG_CACHE_HOME"/python/
- Reload the environment (logout should work)
Virtual environments
You must use virtual environments for all your python development. It is not optional.
$ python -m venv env
$ source env/bin/activate.fish # depending on the shell of choice
$ pip install number # e.g.
$ pip freeze > requirements.txt # to list all dependencies and their version into a file
$ pip install -r requirements.txt # to install all required dependencies