Mailing List Archive

Where to keep local Python modules?
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).


--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules? [ In reply to ]
Hello,

pe 23. heinäk. 2021 klo 21.44 Chris Green (cl@isbd.net) kirjoitti:

> 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).
>
> Under recent Linux distros there is $HOME/.local/ containing bin and lib
directories. May be it's ok to use this for user-specific python settings
under Linux.

E.g. in .bashrc
export PYTHONPATH=$HOME/.local/python

Result can be checked then in Python:
python
>>> import sys
>>> sys.path
['', '/home/someuser/.local/lib/python', '/usr/lib64/python39.zip',
'/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload',
'/usr/local/lib/python3.9/site-packages',
'/usr/lib64/python3.9/site-packages', '/usr/lib/python3.9/site-packages']

BR,
Roland




>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules? [ In reply to ]
On my Arch Linux box, slightly different path, but still in .local/bin:

pbryan@dynamo:~$ python3
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/pbryan/.local/lib/python3.9/site-packages', '/usr/lib/python3.9/site-packages']
>>>


On Fri, 2021-07-23 at 22:46 +0300, Roland Mueller via Python-list
wrote:
> Hello,
>
> pe 23. heinäk. 2021 klo 21.44 Chris Green (cl@isbd.net) kirjoitti:
>
> > 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).
> >
> > Under recent Linux distros there is $HOME/.local/ containing bin
> > and lib
> directories. May be it's ok to use this for user-specific python
> settings
> under Linux.
>
> E.g. in .bashrc
> export PYTHONPATH=$HOME/.local/python
>
> Result can be checked then in Python:
> python
> > > > import sys
> > > > sys.path
> [.'', '/home/someuser/.local/lib/python', '/usr/lib64/python39.zip',
> '/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload',
> '/usr/local/lib/python3.9/site-packages',
> '/usr/lib64/python3.9/site-packages', '/usr/lib/python3.9/site-
> packages']
>
> BR,
> Roland
>
>
>
>
> >
> > --
> > Chris Green
> > ·
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

--
https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules? [ In reply to ]
Roland Mueller <roland.em0001@googlemail.com> wrote:
> Hello,
>
> pe 23. heinäk. 2021 klo 21.44 Chris Green (cl@isbd.net) kirjoitti:
>
> > 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).
> >
> > Under recent Linux distros there is $HOME/.local/ containing bin and lib
> directories. May be it's ok to use this for user-specific python settings
> under Linux.
>
> E.g. in .bashrc
> export PYTHONPATH=$HOME/.local/python
>
> Result can be checked then in Python:
> python
> >>> import sys
> >>> sys.path
> [.'', '/home/someuser/.local/lib/python', '/usr/lib64/python39.zip',
> '/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload',
> '/usr/local/lib/python3.9/site-packages',
> '/usr/lib64/python3.9/site-packages', '/usr/lib/python3.9/site-packages']
>
That's certainly a possibility, thanks.


--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules? [ In reply to ]
On 23Jul2021 11:33, Chris Green <cl@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@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@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules? [ In reply to ]
Cameron Simpson <cs@cskk.id.au> wrote:
>
> 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.
>
Yes, I think maybe something in a 'lib' directory somewhere makes
sense. I like to keep my home directory as uncluttered as possible
though so maybe I'll keep the lib directory under ~/.config or
~/.local.

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list