Table of Contents
Research
System wide startup
Setting up configuration for all invocations of python is described in the Python3 docs here.
import site site.getusersitepackages() # '/home/benrudgers/.local/lib/python3.6/site-packages'
Interactive startup
This file is the source for startup.py
which could be invoked for my BASH sessions based on the configuration in ~/.profile
:
export PYTHONSTARTUP="$HOME/python/startup/startup.py
Unfortuneately, this doesn't seem to really help Emacs and while there is probably a setting somewhere, it is simpler to sudo nano
the configuration into /etc/environment
as:
PYTHONSTARTUP="/home/benrudgers/python/startup/startup.py
Of course, this probably isn't the best solution if I were using lots of users. Fortunately, I probably came to Linux to late in life to have developed craziness in that regard. Email accounts are another story…but I digress and isn't that what literate programming is for?
Loading a module by path name
There are more descriptions of how to do this than you can shake a stick at. The top answer is:
import importlib.util spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) foo.MyClass()
The simple answer isthe one I want for using the Python REPL:
import sys # the mock-0.3.1 dir contains testcase.py, testutils.py & mock.py sys.path.append('/foo/bar/mock-0.3.1')
Implementation
shell helpers
If I am going to use Python, I want it to help me use my computer and not merely program it. Sometimes I just want commands instead of objects…but I suppose I will settle for command like functions.
import os from os import getcwd as pwd from os import chdir as cd from os import listdir as ls from os import mkdir as mkdir
using
using
is a module designed to capture some of the silky goodness of the :use
keyword in Common Lisp's defpackage
. It is located here.
from sys import path as path path.append('/home/benrudgers/python/using') from using import using as using