Example of using the Python API?

645 views
Skip to first unread message

Seth Lippman

unread,
Feb 19, 2016, 2:14:45 PM2/19/16
to rez-config
Does anyone have an example of using REZ's Python API? I looked around for documentation, and took a look through this google group, but the only thing I have found is this:

]$ rez-bind rez created package 'rez-2.0.0' in /home/ajohns/packages # Now we can resolve a rez environment, and use the API ]$ rez-env rez -- python -c 'import rez; print rez.__version__' 2.0.0

.. which worked for me, and I have started looking through the REZ source code to find which methods I could be using, but I also thought I'd describe the process I am trying to implement and see if anyone has done something similar (or if its even possible)

I have a stand alone python module that I would like to import other python modules into. I would like to use REZ to create a context for those imports, without launching the initial context with the command line tools.

Typically I would do something like:

]$ rez-env cool_module-2.0 -- run_this_command
or
]$ rez-env cool_module-3.0 -- python
>>>from cool_module import coolness

but, in this case, I would like to be able to do the rez-env call from within the python script, without having to launch the python script initially within a REZ context. Is that something that is possible? Something like this:

rez-env rez -- python
>>> import rez
>>> ctx = rez.get_context('cool_module-2+')
>>> ctx.run_pacakge_commands()
>>> from cool_module import coolness

I'm not sure how that would work if the rez-context I requested had a requirement conflict with the version of Python I happened to be running - Maybe that would just return an error of some sort: "unable to resolve requested context" ..

Thanks for any help, I'm open to suggestions of different approaches to this problem as well!

Seth


Allan Johns

unread,
Feb 19, 2016, 3:50:57 PM2/19/16
to rez-c...@googlegroups.com
Hey,

You can't really 'replace' the current env like this (which is what I assume you're suggesting with the "from cool_module..." line). What you can do, is create a context and run a command inside of it, or otherwise inspect it:

>>> from rez.resolved_context import ResolvedContext
>>> c = ResolvedContext(["cool_module-2+"])
>>> retcode, out, err = c.execute_shell(command=["which", "cool_tool"], block=True)
>>> print out
/foo/packages/cool_module/bin/cool_tool

I have thought about adding the kind of functionality that you're talking about, by providing a new class (PythonContext?) that keeps a TcpServer open in the context subshell, and lets you access attributes on the object (modules etc) as if you're in a normal python session. However you'd have to be careful not to use data types (classes etc) that either aren't defined in the current python session, or whose definition is different in the subcontext. It'd be pretty snazzy though.

If all you really want is to run some executable python tool within an env, there's a tool coming soon called "rez-runner" that will allow you to do that. It'll look something like this:


# in file 'cool_tool'...
#!/usr/bin/env rez-runner
#> program: python
#> request: cool_module-2+ my_utils-3.2

import cool_module
print cool_module.__version__


Of course, if you don't want to wait, and you don't need to dynamically resolve a new env every time, you could just create a suite, which is really easy to do using rez-context/rez-suite. Then when you run your cool_tool it'll just automagically run in the correct env.

A




--
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