导入图形 Python 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3730616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:27:30  来源:igfitidea点击:

Error Importing Graphics Python

pythongraphicsmacos

提问by Sneaks McGurk

I'm typing at the console

我在控制台打字

from graphics import * 

and I get this error

我收到这个错误

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from graphics import *
ImportError: No module named graphics

回答by gtrak

did you follow the instructions to install the graphics module and is it in your pythonpath?

您是否按照说明安装图形模块,它是否在您的 pythonpath 中?

回答by duffymo

Perhaps you forgot to run python setup.py -installafter downloading the module.

也许你python setup.py -install下载模块后忘记运行了。

回答by Ray

You can also put graphics.py in the same folder where you started the python shell. http://mcsp.wartburg.edu/zelle/python/graphics.py

您还可以将 graphics.py 放在启动 python shell 的同一文件夹中。 http://mcsp.wartburg.edu/zelle/python/graphics.py

回答by cyvarza

I recently had this issue where I typed (and pasting the code into the interpreter did not work either) 'import graphics' into the interpreter and the error I got was:

我最近遇到了这个问题,我输入(并将代码粘贴到解释器中也不起作用)“导入图形”到解释器中,我得到的错误是:

Traceback (most recent call last): File "", line 1, in from graphics import * ImportError: No module named graphics

回溯(最近一次调用):文件“”,第 1 行,来自图形导入 * 导入错误:没有名为图形的模块

I took the following steps to fix the error:

我采取了以下步骤来修复错误:

-Go to: http://mcsp.wartburg.edu/zelle/python/-Right click on the link 'graphics.py and click 'save target as' -save in the directory 'Python33', along with the 'chaos.py' file. This step is important that you do not save it in other locations (it may work if you save it in other locations I decided to save it in this location).

-转到:http: //mcsp.wartburg.edu/zelle/python/ -右键单击链接“graphics.py”并单击“目标另存为”-保存在目录“Python33”中,以及“混乱”。 py'文件。这一步很重要,不要将其保存在其他位置(如果您将其保存在其他位置,我决定将其保存在此位置可能会起作用)。

Hope this helps.

希望这可以帮助。

回答by James Barnard

I think your question comes from trying to work through Python Programmingby John Zelle, with a Macintosh.

我认为您的问题来自尝试使用 Macintosh 完成 John Zelle 的Python 编程

The first 4 chapters are great. But in chapter 5, where I tended to get stuck, he introduces Objects. The way he does that is kind of interesting. He has created a python module which he calls “graphics.py” which you can download from his website.

前四章很好看。但是在第 5 章中,我经常陷入困境,他介绍了对象。他这样做的方式有点意思。他创建了一个名为“graphics.py”的 Python 模块,您可以从他的网站上下载。

This module (I've attached a link to it below) is a python program written by Mr. Zelle. It creates the tools for making very simple shapes and getting used to the basic concepts with graphics and it also serves as a more tangible way of introducing Objects.

这个模块(我在下面附上了一个链接)是一个由 Zelle 先生编写的 python 程序。它创建了用于制作非常简单的形状和熟悉图形基本概念的工具,它也作为一种更切实的方式介绍对象。

However I was confused, it took me a while to realize that “graphics.py” was a pedagogical program Mr. Zelle created, and not something that comes bundled with MacPython. This confusion stems from the fact that the programs in Chapter 5 all begin with “import graphics” which looks very similar to the “import math” command at the beginning of every program in the 3rd chapter.

然而我很困惑,我花了一段时间才意识到“graphics.py”是 Zelle 先生创建的一个教学程序,而不是与 MacPython 捆绑在一起的东西。这种混淆源于第 5 章中的程序都以“导入图形”开头,这看起来与第 3 章中每个程序开头的“导入数学”命令非常相似。

The key difference is that “import math” imports the standard mathematics library that came pre-bundled with MacPython. But “import graphics” refers to John Zelle's own “graphics.py” module, which you have to download and instal first.

关键区别在于“import math”导入了与 MacPython 预先捆绑在一起的标准数学库。但是“导入图形”指的是 John Zelle 自己的“graphics.py”模块,您必须先下载并安装该模块。

It took me a while to figure that out….

我花了一段时间才弄明白……

Then once I did, I went to his website, copied the program from this website:

然后一旦我做了,我就去他的网站,从这个网站复制程序:

http://mcsp.wartburg.edu/zelle/python/graphics.py

http://mcsp.wartburg.edu/zelle/python/graphics.py

...into IDLE and then saved it as graphics.py

...进入空闲,然后将其保存为 graphics.py

This is where it got maddening...

这就是让人抓狂的地方……

On Windows if you just put the graphics.py file in the same folder as Python, it can find the file and use it without a problem.

在 Windows 上,如果你只是将 graphics.py 文件放在与 Python 相同的文件夹中,它可以毫无问题地找到该文件并使用它。

Here is what the book said, that made me feel so crazy:

这本书是这么说的,让我觉得很疯狂:

“To use the graphics module (graphics.py) you need to place this file where Python can locate it. >One simple approach is to put it in the same folder where you keep your Python programs. Starting >Python in this folder will also let you import the graphics library to experiment interactively. Alternatively, you can place the graphics.py file in a system-wide directory so that it is >available for import no matter what directory Python starts in. The standard directory for placing >local additions to Python is the site-packages directory. On my Windows installation, the complete >path to the folder is:

C:\Python23\Lib\site-packages

On my Linux system, the file resides in:

/usr/local/bin/lib/python2.3/site-packages."

“要使用图形模块 (graphics.py),您需要将此文件放在 Python 可以找到它的地方。>一种简单的方法是将它放在保存 Python 程序的同一个文件夹中。在此文件夹中启动 >Python 还可让您导入图形库以进行交互实验。或者,您可以将 graphics.py 文件放在系统范围的目录中,以便无论 Python 在哪个目录启动,它都可用于导入。放置 Python 本地添加的标准目录是 site-packages 目录。在我的 Windows 安装中,文件夹的完整路径是:

C:\Python23\Lib\site-packages

在我的 Linux 系统上,该文件位于:

/usr/local/bin/lib/python2.3/site-packages。”

On a windows OS, all that you have to do is go to Python.org and download Python for Windows and put that graphics.py file in the main folder, and boom, you're golden, NOT SO FOR MACINTOSH!!!!

在 Windows 操作系统上,您所要做的就是访问 Python.org 并下载适用于 Windows 的 Python 并将该 graphics.py 文件放在主文件夹中,然后繁荣,您是金色的,对于 MACINTOSH 来说并非如此!!!!

2 years ago, this is where I got totally stuck, because I had no idea about site paths, or directories; I just pointed and clicked; I didn't know about the Unix system underneath the Macintosh Aqua GUI.

2 年前,这是我完全陷入困境的地方,因为我不知道站点路径或目录;我只是指出并点击; 我不知道 Macintosh Aqua GUI 下的 Unix 系统。

And the book gives no instructions for what to do if you have a Macintosh, and I hit a wall.

如果你有一台 Macintosh,这本书没有给出该怎么做的说明,而我碰壁了。

But when I went at it again a few weeks ago, those directories made more sense to me, because I spent the spring and summer playing with a guidebook to using the UNIX command shell on my Mac.

但是当我几周前再次访问它时,这些目录对我来说更有意义,因为我整个春天和夏天都在玩一本在 Mac 上使用 UNIX 命令 shell 的指南。

So I realized that my problem had nothing to do with Tkinter, It was just that graphics.py needed to be in the right directory. I couldn't just put it in the folder next to IDLE as I could on a Windows machine. I needed to find the right directory.

所以我意识到我的问题与 Tkinter 无关,只是 graphics.py 需要在正确的目录中。我不能像在 Windows 机器上那样把它放在 IDLE 旁边的文件夹中。我需要找到正确的目录。

Since OSX is built on a UNIX base, I thought that the file path might be the same as Linux. Sadly, there was no “local”, “bin” after the “user” directory. But its not quite the same in OSX.

由于 OSX 是基于 UNIX 构建的,我认为文件路径可能与 Linux 相同。遗憾的是,在“user”目录之后没有“local”、“bin”。但它在 OSX 中并不完全相同。

Instead you can use IDLE itself to find out which path directories it uses.

相反,您可以使用 IDLE 本身来找出它使用的路径目录。

You type this:

你输入这个:

import sys

print sys.path

导入系统

打印 sys.path

and BOOM, it spits out a whole bunch of directories, confusingly formatted like this

和 BOOM,它吐出一大堆目录,格式混乱,像这样

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/平台 mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

But when I looked closer I noticed the directory “site-packages” which looked a lot like the “site-packages” on the Linux and Windows command lines. So I pulled out that directory chain

但是当我仔细观察时,我注意到目录“ site-packages”,它看起来很像Linux 和 Windows 命令行上的“ site-packages”。所以我拉出那个目录链

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',‘ /Library/Python/2.7/site-packages']

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/平台 mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages','/Library/Python/2.7/site-packages']

Then using the UNIX shell I followed it down, and then I did the same thing in the Aqua interface.

然后使用 UNIX shell 我跟着它,然后我在 Aqua 界面中做了同样的事情。

And there, buried 8 levels down in the directory, amidst hundreds of other files, I placed my graphics.py file.

在那里,在目录下 8 层,在数百个其他文件中,我放置了我的 graphics.py 文件。

Then I went back into IDLE, typed in “import graphics”

然后我回到 IDLE,输入“导入图形”

AND IT WORKED!!

它奏效了!!

If you are having this problem. I hope this solution saves you my headache.

如果您遇到此问题。我希望这个解决方案能让你免于头疼。

回答by Christopher

Short cut to installing graphics.pyvia command line and/or terminal window. run the following command in admin rights in windows. this will allow you to import graphics

通过命令行和/或终端窗口安装graphics.py 的捷径。在 Windows 中以管理员权限运行以下命令。这将允许您导入图形

pip3 install --user http://bit.ly/csc161graphics

pip3 install --user http://bit.ly/csc161graphics

ps. Im running python 3.6.2 on Atom. Hope this helps some.

附:我在 Atom 上运行 python 3.6.2。希望这对一些人有所帮助。