selecting-python.txt 6.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
Controlling which Python to use
-------------------------------

The following assumes that your $HOME/.buildout/default.cfg has
python2.3 and python2.4 sections that define Python 2.3 and Python 2.4
executables.

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.

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
We have a link server:

    >>> print get(link_server),
    <html><body>
    <a href="demo-0.1-py2.3.egg">demo-0.1-py2.3.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.3.egg">demo-0.2-py2.3.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.3.egg">demo-0.3-py2.3.egg</a><br>
    <a href="demo-0.3-py2.4.egg">demo-0.3-py2.4.egg</a><br>
    <a href="demoneeded-1.0-py2.3.egg">demoneeded-1.0-py2.3.egg</a><br>
    <a href="demoneeded-1.0-py2.4.egg">demoneeded-1.0-py2.4.egg</a><br>
    <a href="demoneeded-1.1-py2.3.egg">demoneeded-1.1-py2.3.egg</a><br>
    <a href="demoneeded-1.1-py2.4.egg">demoneeded-1.1-py2.4.egg</a><br>
    <a href="index/">index/</a><br>
    <a href="other-1.0-py2.3.egg">other-1.0-py2.3.egg</a><br>
    <a href="other-1.0-py2.4.egg">other-1.0-py2.4.egg</a><br>
    </body></html>
30 31 32 33 34 35 36 37 38 39

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

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts = demo
    ... eggs-directory = eggs
    ...
jim's avatar
jim committed
40 41 42
    ... [python2.3]
    ... executable = %(python23)s
    ...
43 44
    ... [demo]
    ... recipe = zc.recipe.egg
45
    ... eggs = demo <0.3
46 47
    ... find-links = %(server)s
    ... index = %(server)s/index
48
    ... python = python2.3
49
    ... interpreter = py-demo
jim's avatar
jim committed
50
    ... """ % dict(server=link_server, python23=python2_3_executable))
51 52 53 54 55 56 57

Now, if we run the buildout:

    >>> import os
    >>> os.chdir(sample_buildout)
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
    >>> print system(buildout),
58 59 60 61 62
    buildout: Installing demo
    zc.buildout.easy_install: Getting new distribution for demo<0.3
    zc.buildout.easy_install: Got demo 0.2
    zc.buildout.easy_install: Getting new distribution for demoneeded
    zc.buildout.easy_install: Got demoneeded 1.1
63 64 65 66 67

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

    >>> ls(sample_buildout, 'eggs')
    -  demo-0.2-py2.3.egg
68
    -  demoneeded-1.1-py2.3.egg
jim's avatar
jim committed
69 70
    -  setuptools-0.6-py2.4.egg
    -  zc.buildout-1.0-py2.4.egg
71 72 73
 
And the generated scripts invoke Python 2.3:

jim's avatar
jim committed
74 75 76 77 78 79
    >>> 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))
80 81
    >>> f.readline().strip() == '#!' + python2_3_executable
    True
jim's avatar
jim committed
82
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
83 84 85 86
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demo-0.2-py2.3.egg',
87
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demoneeded-1.1-py2.3.egg',
88 89 90 91 92 93 94
      ]
    <BLANKLINE>
    import eggrecipedemo
    <BLANKLINE>
    if __name__ == '__main__':
        eggrecipedemo.main()

jim's avatar
jim committed
95 96 97 98
    >>> 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'))
99
    >>> f.readline().strip() == '#!' + python2_3_executable
100
    True
jim's avatar
jim committed
101
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
102
    import sys
103
    <BLANKLINE>
104
    sys.path[0:0] = [
jim's avatar
jim committed
105
      '/tmp/tmp5zS2Afsample-buildout/eggs/demo-0.2-py2.3.egg',
106
      '/tmp/tmp5zS2Afsample-buildout/eggs/demoneeded-1.1-py2.3.egg',
107
      ]
108
    <BLANKLINE>
jim's avatar
jim committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    _interactive = True
    if len(sys.argv) > 1:
        import getopt
        _options, _args = getopt.getopt(sys.argv[1:], 'ic:')
        _interactive = False
        for (_opt, _val) in _options:
            if _opt == '-i':
                _interactive = True
            elif _opt == '-c':
                exec _val
    <BLANKLINE>
        if _args:
            sys.argv[:] = _args
            execfile(sys.argv[0])
    <BLANKLINE>
    if _interactive:
        import code
        code.interact(banner="", local=globals())

    >>> f.close()
129 130 131 132 133 134 135 136 137 138 139

If we change the Python version to 2.4, we'll use Python 2.4 eggs:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts = demo
    ... eggs-directory = eggs
    ...
    ... [demo]
    ... recipe = zc.recipe.egg
140
    ... eggs = demo <0.3
141 142
    ... find-links = %(server)s
    ... index = %(server)s/index
143
    ... python = python2.4
144
    ... interpreter = py-demo
jim's avatar
jim committed
145 146 147 148 149
    ...
    ... [python2.4]
    ... executable = %(python24)s
    ...
    ... """ % dict(server=link_server, python24=python2_4_executable))
150 151

    >>> print system(buildout),
152 153 154 155 156 157
    buildout: Uninstalling demo
    buildout: Installing demo
    zc.buildout.easy_install: Getting new distribution for demo<0.3
    zc.buildout.easy_install: Got demo 0.2
    zc.buildout.easy_install: Getting new distribution for demoneeded
    zc.buildout.easy_install: Got demoneeded 1.1
158 159 160 161

    >>> ls(sample_buildout, 'eggs')
    -  demo-0.2-py2.3.egg
    -  demo-0.2-py2.4.egg
162 163
    -  demoneeded-1.1-py2.3.egg
    -  demoneeded-1.1-py2.4.egg
jim's avatar
jim committed
164 165
    -  setuptools-0.6-py2.4.egg
    -  zc.buildout-1.0-py2.4.egg
166

jim's avatar
jim committed
167 168 169 170
    >>> if sys.platform == 'win32':
    ...     f = open(os.path.join(sample_buildout, 'bin', 'demo-script.py'))
    ... else:
    ...     f = open(os.path.join(sample_buildout, 'bin', 'demo'))
171 172
    >>> f.readline().strip() == '#!' + python2_4_executable
    True
jim's avatar
jim committed
173
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
174 175 176 177
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demo-0.2-py2.4.egg',
178
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demoneeded-1.1-py2.4.egg',
179 180 181 182 183 184 185
      ]
    <BLANKLINE>
    import eggrecipedemo
    <BLANKLINE>
    if __name__ == '__main__':
        eggrecipedemo.main()

jim's avatar
jim committed
186 187 188 189 190 191
    >>> f.close()

    >>> 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'))
192
    >>> f.readline().strip() == '#!' + python2_4_executable
193
    True
jim's avatar
jim committed
194
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
195
    import sys
196
    <BLANKLINE>
197
    sys.path[0:0] = [
jim's avatar
jim committed
198
      '/tmp/tmp5zS2Afsample-buildout/eggs/demo-0.2-py2.4.egg',
199
      '/tmp/tmp5zS2Afsample-buildout/eggs/demoneeded-1.1-py2.4.egg',
200
      ]
201
    <BLANKLINE>
jim's avatar
jim committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    _interactive = True
    if len(sys.argv) > 1:
        import getopt
        _options, _args = getopt.getopt(sys.argv[1:], 'ic:')
        _interactive = False
        for (_opt, _val) in _options:
            if _opt == '-i':
                _interactive = True
            elif _opt == '-c':
                exec _val
    <BLANKLINE>
        if _args:
            sys.argv[:] = _args
            execfile(sys.argv[0])
    <BLANKLINE>
    if _interactive:
        import code
        code.interact(banner="", local=globals())

    >>> f.close()