Import of Pex File Environment

671 views
Skip to first unread message

Serge

unread,
Jul 3, 2015, 8:43:56 AM7/3/15
to pants...@googlegroups.com
Hi,

I wanted to know if there is a way to access the packages/modules that are available within a pex file from another python script.

So, for example:

First, I got a module mymodule.py, with the following code:

if __name__ == '__main__': main() def main(): print "this line will be printed!"

Second, I build a PEX file mypex.pex that contains the module mymodule.py

Third, in another script test.py, I'd like to reuse the module mymodule.py, but importing it from from the PEX file

I'd like to to something like that

importedModule = < function that "loads the pex file environment">
importedModule.main()

Thks in advance,

Brian Wickman

unread,
Jul 3, 2015, 2:27:32 PM7/3/15
to Serge, pants-devel
There are a couple of ways to do this now.

1) PEX_PATH (preferred as of pex 1.0)

If you're invoking from a pex, you can use PEX_PATH to merge together multiple pex files into a single runtime environment.  It would look something like this:

pex -o empty.pex         # create pex file with no dependencies
pex ... -o mymodule.pex  # create pex file with mymodule.main
pex flask boto psutil -o mydeps.pex # bundle up whatever other dependencies you may have

then you can do

PEX_PATH=mymodule.pex:mydeps.pex:... ./empty.pex

This launches the empty environment but then merges in all the dependencies from 'mymodule' and 'mydeps' and whichever other pex files you have.  You can then 'import mymodule' and call mymodule.main.  This of course means that you need to be launching your interpreter via a pex file, but that's usually not a bad idea anyway (unless you have no other options, e.g. when using a web/wsgi framework.)


2) bootstrap_pex_env

If you have the location to a pex file, you can activate the environment through some path hackery:

path_to_pex_file = '/usr/local/libexec/mymodule.pex'
sys.path.insert(0, os.path.join(path_to_pex_file, '.bootstrap'))
from _pex.pex_bootstrapper import bootstrap_pex_env
bootstrap_pex_env(path_to_pex_file)

import mymodule
mymodule.main()

Alternately if you have 'pex' installed in your environment, you can just do 'from pex.pex_bootstrapper import bootstrap_pex_env' and do the same thing, but then you need to ensure that the pex file was built with the pex from your environment, otherwise there may be incompatibilities.

Hope that helps,
brian


Reply all
Reply to author
Forward
0 new messages