[Scons-users] Build, install and use python modules from scons

Bobby Casey beecee808 at gmail.com
Mon Oct 17 17:26:38 EDT 2016


Hello,

I'm running into an issue with setting environment variables for
actions.  Essentially what I'm
trying to do is install some python modules in my build directory and
set PYTHONPATH for any python
scripts that are getting executed, but I can't find a good way.  I put
an example setup below:

./SConstruct ./module-test.scons ./module_test.py ./extern/module_a/setup.py
./extern/module_a/module_a/__init__.py ./extern/module_b/setup.py
./extern/module_b/module_b/__init__.py

I should note that all directories under `extern' are pulled in from
an external repository.  We
need to build/use those in order to properly build our application.

module-test.scons looks like this:

```
env = Environment() env['PYLIBSDIR'] = env.Dir('pylibs') def
install_python_module(mdir, mname):
source = env.Dir(mdir).srcnode() target = '%s/%s' % (env['PYLIBSDIR'],
mname) lib = Command(target,
source, [Delete('${TARGET.abspath}'), 'PYTHONPATH=%s python setup.py
build_py -d build' %
env['PYLIBSDIR'].abspath, Mkdir(env['PYLIBSDIR']),
Copy('${TARGET.abspath}', mname)], chdir=source)
return lib

modules = install_python_module('module_a', 'module_a') modules +=
install_python_module('module_b',
'module_b')

module_test_file = env.File('module_test.py') test_out =
Command(target='test.out',
source='test.in', action='PYTHONPATH=%s %s < $SOURCE > $TARGET' %
(env['PYLIBSDIR'].path,
module_test_file.path))

env.Depends(module_test_file, modules) env.Depends(test_out, module_test_file)
```

module_test.py will attempt to import module_a and module_b.  What I
want to do is build module_a
and module_b (using `python setup.py build_py') and install them as
modules in the `pylibs'
directory, where it can be used by other scripts. I then need to run
my other script
(module_test.py) with PYTHONPATH set properly to find these modules.
I'm presently doing this by
explicitly setting PYTHONPATH as part of my action, which feels really
clumsy and error prone.  Is
there a better way to do this?

To recap, I think these are my two core questions:

1. My method of installing libraries (the install_python_modules
function above) feels wrong.  Is
there a better way?

2. Is there a better way to pass environment variables down to called
applications?  Putting
PYTHONPATH on the command line is probably non-portable and feels like
one big hack.

Thanks,
  Bobby


More information about the Scons-users mailing list