Python3安装turtle全教程

说明

最近需要使用Python2中的turtle画图,使用默认的pip install turtle无法安装成功,会报错,下载源码安装成功,特此记录。

步骤

首先,下载官网turtle源码,官网地址:turtle-0.0.2

这时,直接解压后安装是无法安装的,因为turtle是Python2的库,语法是Python2,需要修改

修改文件setup.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python



# Copyright (c) 2008-2009 Adroll.com, Valentino Volonghi.

# See LICENSE for details.



"""

Distutils installer for turtle.

"""



try:

    # Load setuptools, to build a specific source package

    import setuptools

except ImportError:

    pass



# For great justice, take off every zig.

import sys, os, pprint, traceback



import turtle

version = turtle.__version__



install_requires = ["Twisted>=8.0.1", "PyYAML>=3.0.8"]



setup = setuptools.setup

find_packages = setuptools.find_packages



# Most of the following code is from Divmod Epsilon, (C) 2008 Divmod, Inc.

# under MIT license, see http://divmod.org/trac/wiki/DivmodEpsilon

# since I only need this to install turtle correctly with Twisted

# Plugins I'd rather copy this few lines than actually add another

# dependency. Also this version uses the setuptools setup() function.



def pluginModules(moduleNames):

    from twisted.python.reflect import namedAny

    for moduleName in moduleNames:

        try:

            yield namedAny(moduleName)

        except ImportError:

            pass

        except ValueError, ve:

            if ve.args[0] != 'Empty module name':

                traceback.print_exc()

        except:

            traceback.print_exc()



def _regeneratePluginCache(pluginPackages):

    ## print 'Regenerating cache with path: ',

    ## pprint.pprint(sys.path)

    from twisted import plugin

    for pluginModule in pluginModules([

        p + ".plugins" for p in pluginPackages]):

        # Not just *some* zigs, mind you - *every* zig:

        #print 'Full plugin list for %r: ' % (pluginModule.__name__)

        list(plugin.getPlugins(plugin.IPlugin, pluginModule))



def regeneratePluginCache(dist, pluginPackages):

    if 'install' in dist.commands:

        sys.path.insert(0, os.path.abspath(dist.command_obj['install'].install_lib))

        _regeneratePluginCache(pluginPackages)



def autosetup(**kw):

    packages = []

    datafiles = {}

    pluginPackages = []



    for (dirpath, dirnames, filenames) in os.walk(os.curdir):

        dirnames[:] = [p for p in dirnames if not p.startswith('.')]

        pkgName = dirpath[2:].replace('/', '.')

        if '__init__.py' in filenames:

            # The current directory is a Python package

            packages.append(pkgName)

        elif 'plugins' in dirnames:

            # The current directory is for the Twisted plugin system

            pluginPackages.append(pkgName)

            packages.append(pkgName)



    for package in packages:

        if '.' in package:

            continue

        D = datafiles[package] = []

        #print 'Files in package %r:' % (package,)

        #pprint.pprint(os.listdir(package))

        for (dirpath, dirnames, filenames) in os.walk(package):

            dirnames[:] = [p for p in dirnames if not p.startswith('.')]

            for filename in filenames:

                if filename == 'dropin.cache':

                    continue

                if (os.path.splitext(filename)[1] not in ('.py', '.pyc', '.pyo')

                    or '__init__.py' not in filenames):

                    D.append(os.path.join(dirpath[len(package)+1:], filename))

    autoresult = {

        'packages': packages,

        'package_data': datafiles,

        }

    #pprint.pprint(autoresult, indent=4)

    assert 'packages' not in kw

    assert 'package_data' not in kw

    kw.update(autoresult)

    distobj = setup(**kw)

    regeneratePluginCache(distobj, pluginPackages)

    return distobj



description = """\

Turtle is an HTTP proxy whose purpose is to throttle connections to

specific hostnames to avoid breaking terms of usage of those API

providers (like del.icio.us, technorati and so on).

"""



autosetup(

    name = "turtle",

    author = "Valentino Volonghi",

    author_email = "valentino@adroll.com",

    url = "http://adroll.com/labs",

    description = description,

    license = "MIT License",

    version=version,

    install_requires=install_requires,

    classifiers = [

        'Development Status :: 4 - Beta',

        'Environment :: Web Environment',

        'Intended Audience :: Developers',

        'License :: OSI Approved :: MIT License',

        'Natural Language :: English',

        'Programming Language :: Python',

        'Topic :: Internet',

    ],

    include_package_data=True,

    zip_safe=False

)

修改上述代码第40行:

1
except ValueError as ve:

修改第20行:

1
version = "0.0.2"

执行安装:

1
2
3
# pip install turtle的路径

pip install D:\turtle-0.0.2

安装成功如图:

成功安装turtle

结语

注意安装时需要使用管理员权限运行powershell,否则会报错。 本文使用Python3.10.4,turtle-0.0.2

使用 Hugo 构建
主题 StackJimmy 设计