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
I think your question comes from trying to work through Python Programming by John Zelle, with a Macintosh.
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.
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.
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.
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.
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
...into IDLE and then saved it as 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.
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."
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!!!!
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.
And the book gives no instructions for what to do if you have a Macintosh, and I hit a wall.
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.
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.
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.
Instead you can use IDLE itself to find out which path directories it uses.
You type this:
import sys
print sys.path
and BOOM, it spits out a whole bunch of directories, confusingly formatted like this
['', '/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’]
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
['', '/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’]
Then using the UNIX shell I followed it down, and then I did the same thing in the Aqua interface.
And there, buried 8 levels down in the directory, amidst hundreds of other files, I placed my graphics.py file.
Then I went back into IDLE, typed in “import graphics”
AND IT WORKED!!
If you are having this problem. I hope this solution saves you my headache.
did you follow the instructions to install the graphics module and is it in your pythonpath?
Type how to install graphics.py in Google.
http://www.google.co.in/search?sourceid=chrome&ie=UTF-8&q=how+tooinstall+gaphics.py#sclient=psy-ab&hl=en&source=hp&q=how+to+install+gaphics.py&pbx=1&oq=how+to+install+gaphics.py&aq=f&aqi=&aql=&gs_sm=s&gs_upl=0l0l0l24645l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=344adf6de4d60025&biw=1366&bih=600
Click on the third link
http://mcsp.wartburg.edu/zelle/python/graphics.py
Copy that file which got downloaded to your lib folder in Python32 in C drive.
The error will not occur now.
Short cut to installing graphics.py via command line and/or terminal window. run the following command in admin rights in windows.
this will allow you to import graphics
pip3 install --user http://bit.ly/csc161graphics
ps. Im running python 3.6.2 on Atom. Hope this helps some.
Perhaps you forgot to run python setup.py -install after downloading the module.
You can also put graphics.py in the same folder where you started the python shell.
http://mcsp.wartburg.edu/zelle/python/graphics.py
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
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).
Hope this helps.
Related
I am working though Zelle's Python book (Python 2.7.5, Canopy Express, Windows 7). For Chapter 5 you need to install his graphics package, graphics.py. I downloaded the current version, put it in the C:\Python27\Lib\site-packages folder and made sure the file has the '.py' extension, but when I type >>import graphics I get the following error in both Canopy and IDLE:
ImportError
Traceback (most recent call last)
<ipython-input-1-e6fd1288a7fc> in <module> ()
----> 1 import graphics
ImportError: No module named graphics
To the best of my knowledge I've followed the instructions and hints on the website and here in StackOverflow, but I'm flummoxed. Any suggestions? I like the Zelle book but I'm not sure how much use it will be from this point on without the graphics package - from just skimming it seems like the majority of the examples and exercises use it. Thanks.
It took a while, but I got it working. Enthought Canopy uses its own (virtual) environments, so you need to put Zelle's graphics package there, not in the system library. I had success by placing graphics in Enthought's top level directory, which you can determine in the interpreter:
>> import site
>> site.getpackages()[0]
I copied the graphics package there, without the .py extension, fired up Canopy and I was off and running. Thanks to everyone who has posted, in various places, tips on importing - eventually it made sense! By the way,
>> site.getpackages()
will give you all site package directories, but I only needed the first.
When I try to import time I get : No module named time
I have tried other time modules(datetime and timeit) and they work fine. I decided to check my installation and I can't find time.py anywhere. I checked the Lib, Scripts, libs and include folders, but can't find it anywhere.
Anyone know what I can do to fix this? Maybe download the .py and put it in Lib myself?
I am using Python 3.3.5 with PyCharm IDE. Only extra scripts I've installed is EasyInstall and PRAW.
The import does work. When PyCharm said No module named time, I assumed I would get a compiler error and started trying to fix it.
However when I eventually just ran the code it worked fine. I expect PyCharm doesn't detect the time module as it's a dll and not a py as noted by Martijn in the comments. This is on PyCharm Community Edition 4.0.4.
I tried playing with virtualenv and a host of other things, but I eventually went to Preferences -> Build, Execution, Deployment -> Console -> Python Console, and in the "starting script" box, I added two lines:
sys.builtin_module_names.append('sys')
sys.builtin_module_names.append('time')
This got rid of errors I had with both sys and time. Once I did that, I even get autocomplete for both of those modules... weird.
I'm using Mountain Lion at the moment.
I've installed Blender (because it's a dependency of OpenGrasp), and downloaded OpenGrasp. However, I try to load the robot editor up and I get this:
$ python GraspRobotEditor.py
Traceback (most recent call last):
File "GraspRobotEditor.py", line 34, in <module>
import Blender
ImportError: No module named Blender
How do I point Python to the Blender python interface? the Getting Started guide doesn't instruct you much here.
(I'm sure this is a trivial problem to solve but I'd like to see this documented on StackOverflow anyway.)
From the error you it can't find the python module Blender which represents the python hook to Blender. So there could be a few This could be any number of reasons to do with your setup.
The first is that the Blender module is runtime generated whileBlender is running. The specific 'Blender' module used is part of the Blender 2.4x series. According to the link you provided there, they mention porting to newer versions but checking their snv the code is definitely written for 2.49.
Blender 2.49b was the last stable release - http://download.blender.org/release/ Ensure you are using that.
The next thing is whether it can be run via the the Run Script command
TL:DR
Start up blender
Switch to the text workspace
Open the script and see if it can be run directly.
If not you will need to install the folder to the Blender modules directory of your install and then try running it.
Edit: Was looking at into the SVN some more and there does appear to be a version 2 for blender 2.5+ which can be put into a zip file and installed via the add-on installer. According to the bl_info it was build for 2.58 but what support level you would need to try out yourself.
I have been banging my head against the wall trying to get Exscript installed. After multiple failed attempts at doing it manually, I installed ActivePython and had success running "pypm install Exscript" from the cmd prompt.
I am now going through the Exscript documentation (found here https://github.com/knipknap/exscript/wiki/Python-API-Tutorial) and if I run the first example script I get an error:
>>> from Exscript.util.interact import read_login
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from Exscript.util.interact import read_login
ImportError: No module named interact
So, I understand that this is saying that there is no module interact. How can I check this? Is there a way I can manually add this module? I would love to know WHY this module didnt come with the package, but that may be impossible to answer :)
Any and all help is greatly appreciated. Thank you
EDIT -
import Exscript.util works but if I try import Exscript.util.Interact I get the error. When I look in util.py I see an entry that says "from FooLib import Interact". I first thought it may just be a capitalization error (Exscript.util.interact vs util.Interact) but neither of those work. I am not sure where to go from here... :(
EDIT -
I have posted this question on the developers forums, hopefully he will have an answer for us. https://github.com/knipknap/exscript/issues/15
EDIT -
The developer suggested that I was using an old version and told me to download the latest. I had struggled installing the module manually so I googled how to easily install py modules. I found a writeup on easy_install.exe. I ran "c:\Python26>easy_install C:\Users\support\Desktop\lou\knipknap-exscript-v2.1-70-gf5583f3.tar.gz" from the cmd prompt, the module was installed (no errors) and now when i run the script it works.
Next challenge will be how to get these scripts to run as stand-alone exe's on users computers without Python installed :)
THANK YOU to everyone to commented I truely appreciate your help.
Lou
One common way packages are installed is as directories. So check your site-packages directory for an Exscript directory, and inside that there should be a util directory, and inside that there should be an interact.py file. Look for similar spellings in case the tutorial misspelled something.
I'm getting this error every time I run any python file in Eclipse using PyDev:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 73, in <module>
__boot()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 2, in __boot
import sys, imp, os, os.path
ImportError: Bad magic number in /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc
I'm using python 2.6. This problem does not occur when I run python from the terminal (2.7 or 2.6). The only substantial thing I've changed since everything last worked, is an update to OSX Lion from Snow Leopard.
Similar discussions to this seem to suggest some kind of removal of the .pyc file, because of some kind of mismatch between what was originally using the .pyc files (I'm not entirely sure what a magic number is...). But I was a bit cautious of the idea of deleting os.pyc from the Frameworks directory. When the only other file is an os.pyo file (not sure what the difference it), rather than an os.py.
I've installed all OSX Lion updates, Eclipse updates and PyDev updates.
This problem occurs even with code such as :
if __name__ == '__main__':
pass
Any help resolving this would be appreciated!
Upgrading Python to 2.7.1, running "Update Shell Profile" command file which is located in Python directory and changing the Python settings in Netbeans according to new installation worked for me.
Yeah, you'll need to regenerate all your *.pyc and *.pyo files from the *.py files.
How you do this depends on how they were generated in the first place. Some packaging of python (and it's add-ons), such as in some Linux distros, gets a little too clever for its own good and keeps the original *.py files somewhere else and have their own build system for generating and placing the *.pyc and/or *.pyo files. In a case like that, you have to use that build system to regenerate them from the original *.py files.
FYI, here are a couple links on *.pyo files. They are the optimized versions of compiled python modules.
On OS X Lion, you should have a os.py file. This is likely the root cause of your error. The os.pyc file was generated from a different version of python than you are running now. Normally, I imagine the python interpreter would just regenerate the file from os.py. But for whatever reason, your system does not have that file.
I suspect that this is a small data point in a larger set of issues and would, in general, recommend a reinstallation of your operating system.
For comparison, I'm running 10.7.1, and I have the following:
[2:23pm][wlynch#orange workout] ls /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.*
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyo
As an aside, the *.pyo file is an optimized version of the python bytecode.