Controlling which Python to use
-------------------------------

The following assumes that you have Python 2.4 installed.

We can specify the python to use by specifying the name of a section
to read the Python executable from.  The default is the section
defined by the python buildout option.

We have a link server:

    >>> print get(link_server),
    <html><body>
    <a href="bigdemo-0.1-py2.4.egg">bigdemo-0.1-py2.4.egg</a><br>
    <a href="demo-0.1-py2.4.egg">demo-0.1-py2.4.egg</a><br>
    <a href="demo-0.2-py2.4.egg">demo-0.2-py2.4.egg</a><br>
    <a href="demo-0.3-py2.4.egg">demo-0.3-py2.4.egg</a><br>
    <a href="demo-0.4c1-py2.4.egg">demo-0.4c1-py2.4.egg</a><br>
    <a href="demoneeded-1.0.zip">demoneeded-1.0.zip</a><br>
    <a href="demoneeded-1.1.zip">demoneeded-1.1.zip</a><br>
    <a href="demoneeded-1.2c1.zip">demoneeded-1.2c1.zip</a><br>
    <a href="extdemo-1.4.zip">extdemo-1.4.zip</a><br>
    <a href="index/">index/</a><br>
    <a href="other-1.0-py2.4.egg">other-1.0-py2.4.egg</a><br>
    </body></html>

We have a sample buildout.  Let's update its configuration file to
install the demo package using Python 2.4.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts = demo
    ... eggs-directory = eggs
    ... index = http://www.python.org/pypi/
    ...
    ... [python2.4]
    ... executable = %(python24)s
    ...
    ... [demo]
    ... recipe = zc.recipe.egg
    ... eggs = demo <0.3
    ... find-links = %(server)s
    ... python = python2.4
    ... interpreter = py-demo
    ... """ % dict(server=link_server, python24=other_executable))

Now, if we run the buildout:

   >>> import os
   >>> os.chdir(sample_buildout)
   >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
   >>> print system(buildout),
   Installing demo.
   Getting distribution for 'demo<0.3'.
   Got demo 0.2.
   Getting distribution for 'demoneeded'.
   Got demoneeded 1.2c1.
   Generated script '/sample-buildout/bin/demo'.
   Generated interpreter '/sample-buildout/bin/py-demo'.

we'll get the Python 2.4 eggs for demo and demoneeded:

    >>> ls(sample_buildout, 'eggs')
    -  demo-0.2-py2.4.egg
    -  demoneeded-1.2c1-py2.4.egg
    d  setuptools-0.6-py2.5.egg
    -  zc.buildout-1.0-py2.5.egg

And the generated scripts invoke Python 2.4:

    >>> import sys
    >>> if sys.platform == 'win32':
    ...    script_name = 'demo-script.py'
    ... else:
    ...    script_name = 'demo'
    >>> f = open(os.path.join(sample_buildout, 'bin', script_name))
    >>> shebang = f.readline().strip()
    >>> if shebang[:3] == '#!"' and shebang[-1] == '"':
    ...     shebang = '#!'+shebang[3:-1]
    >>> shebang == '#!' + other_executable
    True
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/sample-buildout/eggs/demo-0.2-py2.4.egg',
      '/sample-buildout/eggs/demoneeded-1.2c1-py2.4.egg',
      ]
    <BLANKLINE>
    import eggrecipedemo
    <BLANKLINE>
    if __name__ == '__main__':
        eggrecipedemo.main()

    >>> if sys.platform == 'win32':
    ...     f = open(os.path.join(sample_buildout, 'bin', 'py-demo-script.py'))
    ... else:
    ...     f = open(os.path.join(sample_buildout, 'bin', 'py-demo'))

    >>> shebang = f.readline().strip()
    >>> if shebang[:3] == '#!"' and shebang[-1] == '"':
    ...     shebang = '#!'+shebang[3:-1]
    >>> shebang == '#!' + other_executable
    True
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
    <BLANKLINE>
    import sys
    <BLANKLINE>
    sys.path[0:0] = [
      '/sample-buildout/eggs/demo-0.2-py2.4.egg',
      '/sample-buildout/eggs/demoneeded-1.2c1-py2.4.egg',
      ]
    <BLANKLINE>
    _interactive = True
    if len(sys.argv) > 1:
        _options, _args = __import__("getopt").getopt(sys.argv[1:], 'ic:m:')
        _interactive = False
        for (_opt, _val) in _options:
            if _opt == '-i':
                _interactive = True
            elif _opt == '-c':
                exec _val
            elif _opt == '-m':
                sys.argv[1:] = _args
                _args = []
                __import__("runpy").run_module(
                     _val, {}, "__main__", alter_sys=True)
    <BLANKLINE>
        if _args:
            sys.argv[:] = _args
            __file__ = _args[0]
            del _options, _args
            execfile(__file__)
    <BLANKLINE>
    if _interactive:
        del _interactive
        __import__("code").interact(banner="", local=globals())

    >>> f.close()
