How does setup.py work?

Robert Kern robert.kern at gmail.com
Wed Dec 19 04:17:23 EST 2007


dxm wrote:
> I am a new comer to python.
> I am wondering how setup.py works.
> For example, I have a directory like this:
> /
>    setup.py
>    mymodule.c
> 
> where setup.py is:
> 
> from distutils.core import setup, Extension
> 
> mod = Extension('mymodule', sources = ['mymodule.c'])
> 
> setup (name = 'Package',
>        version = '1.0',
>        description = 'This is a demo package',
>        ext_modules = [mod])
> 
> The correct way to install the newly created extension module is to
> type
> python setup.py install instead of executing those statements in
> python shell, isn't it ?

Yes.

> My question is how additional arguments like 'build', 'install' are
> passed into python

Command-line arguments are passed into Python as the list sys.argv. Try running
the following script to explore this:

#### sys_argv.py ####
#!/usr/bin/env python
import sys
print sys.argv
#####################

[~]$ python sys_argv.py
['sys_argv.py']
[~]$ python sys_argv.py build
['sys_argv.py', 'build']
[~]$ python sys_argv.py build_ext --inplace install
['sys_argv.py', 'build_ext', '--inplace', 'install']

http://docs.python.org/lib/module-sys.html

Code inside setup() parses this list to determine what actions the user wants it
to take.

> and how
> can I install it from interactively from python shell

Generally speaking, you don't. distutils was not really designed for this use
case. There is no easy way to do this.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list