selecting-python.txt 6.38 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
jim's avatar
jim committed
49
    ... """ % dict(server=link_server, python23=python2_3_executable))
50 51 52 53 54 55 56 57 58 59 60 61

Now, if we run the buildout:

    >>> import os
    >>> os.chdir(sample_buildout)
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
    >>> print system(buildout),

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

    >>> ls(sample_buildout, 'eggs')
    -  demo-0.2-py2.3.egg
62
    -  demoneeded-1.1-py2.3.egg
jim's avatar
jim committed
63 64
    -  setuptools-0.6-py2.4.egg
    -  zc.buildout-1.0-py2.4.egg
65 66 67
 
And the generated scripts invoke Python 2.3:

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

jim's avatar
jim committed
89 90 91 92
    >>> 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'))
93
    >>> f.readline().strip() == '#!' + python2_3_executable
94
    True
jim's avatar
jim committed
95
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
96
    import sys
97
    <BLANKLINE>
98
    sys.path[0:0] = [
jim's avatar
jim committed
99 100
      '/tmp/tmp5zS2Afsample-buildout/eggs/demo-0.2-py2.3.egg',
      '/tmp/tmp5zS2Afsample-buildout/eggs/demoneeded-1.1-py2.3.egg'
101
      ]
102
    <BLANKLINE>
jim's avatar
jim committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    _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()
123 124 125 126 127 128 129 130 131 132 133

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
134
    ... eggs = demo <0.3
135 136
    ... find-links = %(server)s
    ... index = %(server)s/index
137
    ... python = python2.4
jim's avatar
jim committed
138 139 140 141 142
    ...
    ... [python2.4]
    ... executable = %(python24)s
    ...
    ... """ % dict(server=link_server, python24=python2_4_executable))
143 144 145 146 147 148

    >>> print system(buildout),

    >>> ls(sample_buildout, 'eggs')
    -  demo-0.2-py2.3.egg
    -  demo-0.2-py2.4.egg
149 150
    -  demoneeded-1.1-py2.3.egg
    -  demoneeded-1.1-py2.4.egg
jim's avatar
jim committed
151 152
    -  setuptools-0.6-py2.4.egg
    -  zc.buildout-1.0-py2.4.egg
153

jim's avatar
jim committed
154 155 156 157
    >>> 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'))
158 159
    >>> f.readline().strip() == '#!' + python2_4_executable
    True
jim's avatar
jim committed
160
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
161 162 163 164
    <BLANKLINE>
    import sys
    sys.path[0:0] = [
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demo-0.2-py2.4.egg',
165
      '/private/tmp/tmpOEtRO8sample-buildout/eggs/demoneeded-1.1-py2.4.egg'
166 167 168 169 170 171 172
      ]
    <BLANKLINE>
    import eggrecipedemo
    <BLANKLINE>
    if __name__ == '__main__':
        eggrecipedemo.main()

jim's avatar
jim committed
173 174 175 176 177 178
    >>> 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'))
179
    >>> f.readline().strip() == '#!' + python2_4_executable
180
    True
jim's avatar
jim committed
181
    >>> print f.read(), # doctest: +NORMALIZE_WHITESPACE
182
    import sys
183
    <BLANKLINE>
184
    sys.path[0:0] = [
jim's avatar
jim committed
185 186
      '/tmp/tmp5zS2Afsample-buildout/eggs/demo-0.2-py2.4.egg',
      '/tmp/tmp5zS2Afsample-buildout/eggs/demoneeded-1.1-py2.4.egg'
187
      ]
188
    <BLANKLINE>
jim's avatar
jim committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    _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()