How to preserve sys.path when loading a context in rez-python?

39 views
Skip to first unread message

Brent Villalobos

unread,
Feb 22, 2019, 5:29:21 PM2/22/19
to rez-config
I have a python script that runs via rez-python.  Initially, my sys.path has all the "common" paths found in my python installation like lib/python2.7/site-packages, lib/python2.7/lib-dynload, etc.  However, suppose I create and apply a ResolvedContext object that doesn't load any packages that modify $PYTHONPATH.  Or maybe it does modify $PYTHONPATH but none of the packages are adding things like site-packages and lib-dynload.  After I apply that context, I will no longer be able to import packages like unicodedata because lib-dynload is no longer in sys.path.

I can always make a copy of sys.path before I apply the ResolvedContext and then add it back to sys.path.  But that seems a bit hacky.  In general, our python packages don't add their own lib/python27 directories to $PYTHONPATH since those directories will be added automatically to sys.path when someone runs the python interpreter.  So requesting a python package in our ResolvedContext doesn't work.  I could create a package called something like python_base_libs that adds the various lib/python subdirectories to PYTHONPATH and request that in my ResolvedContext.  But I would like to know if there is a cleaner way of preserving the bundled lib directories of the interpreter being used in the python process regardless of what I do with ResolvedContext.

Allan Johns

unread,
Feb 28, 2019, 4:07:45 PM2/28/19
to rez-c...@googlegroups.com
Hi Brent,

There is not a way to do that currently no. To address this properly, I think there should in general be better control over behavior of path-like variables. Right now the only option is set-on-first-(ap/pre)pend. I think a better approach would be a default mode that does the same thing as currently, but also keeps the pre-rez-env value appended after every modification. Ie, if you had packages A and B with appended python paths A' and B' respectively, then PYTHONPATH would end up something like A':B':<original-py-path>. This is doable, it requires updates to the rex module.

Thx
A





On Sat, Feb 23, 2019 at 9:29 AM Brent Villalobos <brent...@gmail.com> wrote:
I have a python script that runs via rez-python.  Initially, my sys.path has all the "common" paths found in my python installation like lib/python2.7/site-packages, lib/python2.7/lib-dynload, etc.  However, suppose I create and apply a ResolvedContext object that doesn't load any packages that modify $PYTHONPATH.  Or maybe it does modify $PYTHONPATH but none of the packages are adding things like site-packages and lib-dynload.  After I apply that context, I will no longer be able to import packages like unicodedata because lib-dynload is no longer in sys.path.

I can always make a copy of sys.path before I apply the ResolvedContext and then add it back to sys.path.  But that seems a bit hacky.  In general, our python packages don't add their own lib/python27 directories to $PYTHONPATH since those directories will be added automatically to sys.path when someone runs the python interpreter.  So requesting a python package in our ResolvedContext doesn't work.  I could create a package called something like python_base_libs that adds the various lib/python subdirectories to PYTHONPATH and request that in my ResolvedContext.  But I would like to know if there is a cleaner way of preserving the bundled lib directories of the interpreter being used in the python process regardless of what I do with ResolvedContext.

--
You received this message because you are subscribed to the Google Groups "rez-config" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rez-config+...@googlegroups.com.
To post to this group, send email to rez-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/rez-config.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages