Python - rulebook
Published:
Updated:
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
$XDG_CONFIG_HOME/python/startup.py
and add the following code:import atexit import os import readline cachedir = os.getenv("XDG_CONFIG_HOME") histdir = "" if cachedir == "": cachedir = os.path.expanduser("~") histdir = os.path.join(cachedir, ".config", "python") else: histdir = os.path.join(cachedir, "python") os.makedirs(histdir, exist_ok=True) histfile = os.path.join(histdir, "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 the history file at
$XDG_CACHE_HOME/python/history
. - Reload the environment (logout should do).
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
Note You should probably use uv.
This is it for today. I hope you find this article useful. As per usual, if you find any errors, or you want to comment on the article in some other way, you can reply via email.