[Distutils] Question on using distutils.core.run_setup

Phillip J. Eby pje at telecommunity.com
Tue Mar 18 17:34:26 CET 2008


At 06:27 AM 3/18/2008 +0100, Terry Jones wrote:
>[Apologies if this is in the mailing list archives. I looked but didn't
>see anything, based on a search for run_setup]
>
>I'm trying to programmatically install something built using distutils. I
>found distutils.core.run_setup and can use it via
>
>   >>> dist = run_setup('setup.py', ['-q', 'install'])
>
>Is that the recommended way to do an install from inside Python (as opposed
>to doing it on the command line)?

I never could get run_setup() to work robustly.

Here's how setuptools does it, and it works well with quite a lot of packages:

         os.chdir(setup_dir)
         try:
             sys.argv[:] = [setup_script]+list(args)
             sys.path.insert(0, setup_dir)
             DirectorySandbox(setup_dir).run(
                 lambda: execfile(
                     "setup.py",
                     {'__file__':setup_script, '__name__':'__main__'}
                 )
             )
         except SystemExit, v:
             if v.args and v.args[0]:
                 raise
             # Normal exit, just return

This is actually wrapped in a try-finally that saves and restores 
sys.argv, sys.path, os.chdir(), etc.  Have a look at 
setuptools.sandbox.run_setup() for the full source.

Of course, this won't get you access to the distribution object; 
setuptools uses this mainly to run "bdist_egg" on the setup script, 
rather than having it do the installation directly.

(Note, by the way, that distutils setup scripts can get very confused 
if you don't os.chdir() to their directory before running them.)


>If so, how can I find where the thing(s) I installed now resides?

The simplest way would be to use the --record option to "install", to 
write a file listing all the installed files.


>I saw
>dist.packages but that just has top-level package names. I could __import__
>these (and then use module.__file__), but that's not a good solution as it
>may run code I don't want run. On my machine, I can see the packages have
>been installed under the system's python2.5/site-packages directory. But
>how can I determine that programmatically? I don't see anything useful on
>the distutils.dist.Distribution instance I'm getting back from run_setup.

It'd be on the 'install_lib' command instance, not the 
distribution.  Try 
dist.get_finalized_command('install_lib').install_dir instead.




More information about the Distutils-SIG mailing list