python from scratch

I seem to be setting up new computing environments frequently. Here's a recipe for making Python nice.

  1. Use Miniconda to get a current Python distribution. Here is the download page directly.

  2. Don't quite follow all of the detailed instructions. Do run the script you download:

    $ mkdir -p ~/miniconda3
    $ bash ./your-script-name.sh -b -u -p ~/miniconda3
    

    The three shell arguments are:

    -b  Notify of job termination immediately.
    -p  Turned on whenever the real and effective user ids do not match.
    -u  Treat unset variables as an error when substituting.

    (It's easier to find these by doing help set at a terminal than it is by scrolling through the eternal bash man page.)

  3. Do activate the installed distribution. But don't invite to change all of your shell init scripts; we're going to do that on our own

    #### yes, do
    $ source ~/miniconda3/bin/activate
    (conda) $
    #### no, don't!
    ## conda init --all
    
  4. Create two empty virtual environments in your home directory.

    (conda) $ python -m venv ~/.empty_venv
    (conda) $ python -m venv ~/.default_venv
    
  5. Edit your shell startup file, probably .bashrc, to contain the following:

    ## By default, load a terrible, featureless Python .venv
    if [ -f "${HOME}/.empty_venv/bin/activate" ]; then
        source "${HOME}/.empty_venv/bin/activate"
    fi
    
    venv_activate () {
        activate="$1/bin/activate"
        if [ -f "$activate" ]; then
            source "$activate"
        else
            echo file "$activate" not found 1>&2
        fi
    }
    
  6. Now, refresh your terminal:

    (conda) $ exec bash
    (.empty_venv) $
    
  7. Whenever you start a fresh terminal, you'll have this annoying empty Python installation, so something won't work the first time. If you are doing something quick and one-timey everyday stuff, install it in .default_venv:

    (.empty_venv) $ venv_activate ~/.default_venv
    (.default_venv) $ pip install ipython scipy pandas seaborn plotly ...
    

    But when you start a new project and something doesn't work, just put its venv there:

    (.empty_venv) $ cd my/new/project/named/foo
    (.empty_venv) $ python -m venv .venv --prompt .
    (.empty_venv) $ venv_activate .venv
    (foo) $ pip install -r requirements.txt
    

    The --prompt . sets the little string in the prompt to the name of the current directory (at the time the .venv is made), which helps to keep them all your zillion little displosable .venvs straight.