unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: Hartmut Goebel <h.goebel@crazy-compilers.com>
Cc: guix-devel@gnu.org
Subject: Re: 03/163: build/python: Add a new guix-pythonpath procedure.
Date: Fri, 26 Feb 2021 10:36:31 -0500	[thread overview]
Message-ID: <871rd296yo.fsf@gmail.com> (raw)
In-Reply-To: <e32ffb4a-2857-9aa9-e2ac-da9ea79085ee@crazy-compilers.com> (Hartmut Goebel's message of "Fri, 5 Feb 2021 11:26:20 +0100")

Hi Hartmut,

Sorry for the delay.

Hartmut Goebel <h.goebel@crazy-compilers.com> writes:

> Hi Maxim,
>
> many thanks for picking up this issue.
>> Indeed, I thought about the possibility to filter the GUIX_PYTHONPATH
>> entries based on their version at runtime after I wrote my initial
>> reply.  It makes life easier.  I've updated the
>> cu/farewell-to-pythonpath branch with this new way of doing things.
>
> I had a look at the first changes (only) and have some remarks:
>
> 1) Did I understand this correctly: `sitecustomize.py` is filtering
> GUIX_PYTHONPATH for all parts containing
> "/lib/pythonX.Y/site-packages" (with X.Y being Major.Minor)?

Yes.

> 2) This does not remove duplicates and does not honor .pth files in
> the respective directories - which might still be used. Thus
> site.addsitedir() should be called for adding the paths. This also
> takes care about duplicates.

I confess I didn't pay attention to .pth files, which mostly seemed like
legacy cruft to me; are they still used in the context of PEP 517 and
modern Python packaging?  The problem with calling site.addsitedir is
that it simply appends to sys.path.  We want to splice in the content of
GUIX_PYTHONPATH at a controlled location.

> 3) Empty part (…::…) are not handled.

GUIX_PYTHONPATH is not intended for end users; users can and should
still use PYTHONPATH, if they may.  For that reason, I think it's
preferable to keep it as lean as it can be, without burdening it with
unnecessary checks.  Empty sites also don't break anything, should they
appear for any reason:

$ PYTHONPATH="::::$PYTHONPATH" python -c "print('hello\n')"
hello

> 4) Since PYTHONPATH is evaluated prior to importing sitecustomize, any
> sitecustominze.py in the user's path will overwrite our file, thus
> inhibiting our paths to be added. Not sure this is what we want in Guix.

I asked guidance on the #python channel on freenode and was recommended
to use sitecustomize.py for this purpose; reading the doc here seems to
confirm our usage of it is as intended [0]:

    [...] After these path manipulations, an attempt is made to import a
    module named sitecustomize, which can perform arbitrary
    site-specific customizations. It is typically created by a system
    administrator in the site-packages directory.

The last sentence hints at that this is intended for the Python
installation side of things rather than end users.  I've never seen a
Python package with a sitecustomize.py at its root; if I did, I would
consider it bad practice.

> 5) When implementing the logic into site.py, the code could be
> simplified, Eg. You could patch a "getguixsitepackages" (based on
> getsitepackages) and a "addguixsitepackages" (based on
> addguixsitepackages) into site.py. Downside is that maybe we need
> different patches for different Python versions. Benefit is simpler
> code - no need to search the correct position in the list.

I initially went that route, but ended up choosing sitecustomize.py as
it is intended for this purpose and should be more robust in the face of
changes to Python's site.py module.

> 6) Please add some more comments to the code explaining the idea.

I was under the impression the code was concise enough to forego with
verbose explanations; I'd rather keep it this way.  But as the
implementer perhaps I'm not the right person to "see" this!  I would be
happy to consider a patch adding a few comments based on your fresh
perspective, if you think it is worth it.

> Some nitpicking:
>
>> python_root = os.path.realpath(sys.executable).split('/bin/')[0]
>
> There already is sys.prefix, which is also the base for the entry in
> sys.path (see top of site.py

Oh, true!

>> major_minor = '{}.{}'.format(sys.version_info[0], sys.version_info[1])
>
> major_minor = '{}.{}'.format(*sys.version_info)

Neat trick; although the above is a bit more explicit.

>>sys.path = sys.path[:index] + matching_sites + sys.path[index:]
>
> sys.path[index:index] = matching_sites

Neat also!  Thank you for pointing it out.

> I suggest using os.path.join(), os.path.split(), os.pathsep, etc. to
> be forward-compatible. Imagine we want to port Guix to another
> platform with different separators.

If Guix is ever ported to a system not using ':' as the path separator,
it'd be a concern for the search path specification that emits
GUIX_PYTHONPATH rather than here, no?

Thanks for the comments and showing the neat little Python tricks above;
I'd be happy to review a patch if you produce one with these changes and
extra explanatory comments.

Maxim

[0]  https://docs.python.org/3/library/site.html


  reply	other threads:[~2021-02-26 15:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210125070022.22870.17321@vcs0.savannah.gnu.org>
     [not found] ` <20210125070114.03C0B20E1C@vcs0.savannah.gnu.org>
2021-01-25 21:46   ` 03/163: build/python: Add a new guix-pythonpath procedure Ludovic Courtès
2021-01-25 22:10     ` Maxim Cournoyer
2021-01-28 14:16       ` Ludovic Courtès
2021-01-29 14:25         ` Maxim Cournoyer
2021-02-01 15:37           ` Ludovic Courtès
2021-02-05 10:26           ` Hartmut Goebel
2021-02-26 15:36             ` Maxim Cournoyer [this message]
2021-03-07 11:13               ` Hartmut Goebel
2021-03-14  0:58                 ` Maxim Cournoyer
2021-03-27 10:47                   ` Hartmut Goebel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871rd296yo.fsf@gmail.com \
    --to=maxim.cournoyer@gmail.com \
    --cc=guix-devel@gnu.org \
    --cc=h.goebel@crazy-compilers.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).