Where to keep local Python modules?

Cameron Simpson cs at cskk.id.au
Fri Jul 23 19:04:48 EDT 2021


On 23Jul2021 11:33, Chris Green <cl at isbd.net> wrote:
>This isn't a question about how to set PYTHONPATH so that Python code
>can find imported modules, it's about what is a sensible layout for
>one's home directory - i.e. where to put Python modules.
>
>I'm running Linux and have a number of Python modules that are only
>used by my own code.  My top level Python code is all in ~/bin.  I'd
>prefer to separate the modules so that they don't clutter the name
>space.
>
>Currently I have my Python modules in a subdirectory of ~/bin and my
>Python path is set as:-
>
>    PYTHONPATH=/home/chris/bin/pymods
>
>Is this a reasonable approach?  Is there a 'standard' name for the
>directory containing modules, or a standard place for it?  (I don't
>mean a system-wide standard place, I mean a 'my' standard place).

I don't know if this is sensible, but here is what I do.

I use virtualenvs. By default I keep these in ~/var/venv. Here's my 
local MacOS:

    % ls -la ~/var/venv/
    total 0
    drwxr-xr-x   9 cameron  cameron   288  1 Mar 19:47 .
    drwx--S---  41 cameron  cameron  1312 24 Jul 08:27 ..
    lrwxrwxr-x   1 cameron  cameron    14  1 Mar 19:47 3 -> 3.9.2-homebrew
    drwxrwxr-x   8 cameron  cameron   256 14 Jun  2020 3.7.7-homebrew
    drwxrwxr-x   6 cameron  cameron   192 16 Aug  2020 3.8.5-hggit
    drwxrwxr-x   8 cameron  cameron   256 23 Jul  2020 3.8.5-homebrew
    drwxrwxr-x   8 cameron  cameron   256 21 Nov  2020 3.8.6-homebrew
    drwxrwxr-x   8 cameron  cameron   256 28 Dec  2020 3.9.1_2-homebrew
    drwxrwxr-x   9 cameron  cameron   288 26 May 15:17 3.9.2-homebrew

I get my default Pythons from homebrew (again, because I'm on a Mac). So 
the Python came this way:

    % brew install python at 3.9

and the virtualenv got made like this:

    % python3.9 -m venv ~/var/venv/3.9.2-homebrew

giving it a nice detailed directory name.

I keep the convenience symlink "3" pointing at the preferred venv 
(usually the latest).

Then I have ~/var/venv/3/bin in my $PATH.

With that near the front of $PATH so that "python3" and "pip3" come from 
there, modules are installed with pip3, eg:

    % pip3 install cs.upd

They land in the venv, and "python3" (which comes from the venv) knows 
to find them automatically.

Almost everything I use comes either from pip or from my own modules. My 
$PYTHONPATH on the Mac has this:

    /Users/cameron/lib/python:/Users/cameron/rc/python

being, respectively, my personal modules and a place for third party 
modules which do not come from pip. That latter is an empty set, but 
it's there. I doubt you'd need to bother with the latter; I doubt I need 
to either.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list