this is my fist time using the turtle module in python but i can't seem to import it?
Here's my code:
from turtle import *
pen1 = Pen()
pen2 = Pen()
pen1.screen.bgcolour("#2928A7")
and here is the error I get:
Traceback (most recent call last):
File "C:\Python34\Python saves\turtle.py", line 2, in <module>
from turtle import *
File "C:\Python34\Python saves\turtle.py", line 5, in <module>
pen1 = Pen()
NameError: name 'Pen' is not defined
Can anyone tell me what I did wrong?
The problem is that you've named your program "turtle.py".
So when Python sees the statement
from turtle import *
the first matching module named turtle that it finds is your program, "turtle.py".
In other words, your program is basically importing itself and not the turtle graphics module.
Here's some code to demonstrate this problem.
turtle.py
#! /usr/bin/env python
''' Mock Turtle
Demonstrate what happens when you give your program the same name
as a module you want to import.
See http://stackoverflow.com/q/32180949/4014959
Written by PM 2Ring 2015.08.24
'''
import turtle
foo = 42
print(turtle.foo)
help(turtle)
I guess I should show what that code actually prints...
When run as turtle.py it prints the following "help" info:
Help on module turtle:
NAME
turtle - Mock Turtle
FILE
/mnt/sda4/PM2Ring/Documents/python/turtle.py
DESCRIPTION
Demonstrate what happens when you give your program the same name
as a module you want to import.
See http://stackoverflow.com/q/32180949/4014959
Written by PM 2Ring 2015.08.24
DATA
foo = 42
(END)
When you hit Q to get out of the Help, the Help info is displayed again. When you hit Q for the second time, then
42
42
is printed.
Why are the "help" message and 42 printed twice? That's because all the code in turtle.py is executed when it's imported, and then again when its encountered after the import statement. Note that Python doesn't try to import modules that it has already imported (unless explicitly told to do so with reload). If Python did re-import, then the above code would get stuck in an infinite loop of importing.
When run as mockturtle.py it prints:
Traceback (most recent call last):
File "./mock_turtle.py", line 16, in <module>
print(turtle.foo)
AttributeError: 'module' object has no attribute 'foo'
And of course that's because the standard turtle module doesn't actually have a foo attribute.
I think the solution is to type this :
pen1 = turtle.Pen()
Related
I have made a module:
However I got this error when I import the count_kmers function:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'kmer_cython' has no attribute 'count_kmers'
I import my stuff like this:
from Seq_utils.fasta_parser import parse_fasta
from kmer_cython import count_kmers
from Alphabet.alphabet import iupac_dna
My function was defined like this:
It is a simple function that worked fine when I had it spread in the middle of other functions in my directory. Then I decide to organize it and now I wish to run it, but I can't.
I have set the PYTHONPATH in my terminal and other codes that I made a module are working.
Any tip to solve this problem?
Thank you all for your time.
Paulo
I'm new to python and I'm currently learning objects and graphics. I imported the graphics.py file successfully but for some reason it keeps giving me an attribute error whenever I try to run GraphWin. please see below:
import graphics
win = graphics.GraphWin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'graphics' has no attribute 'GraphWin'
I'm using Zelle's "Python Programming: An Introduction to Computer Science" and it's been helpful.
please advise.
Is the graphics.py in the same as the file you’re getting the error in?
Also, try
from graphics import *
Instead of
import graphics
i checked out this article and it proved helpful. i just had to create an extension where i added the specific sub-module to the module folder. in this case it's:
from graphics._init_ import *
win = GraphWin()
this solves the attribute error. however, it only works with the from import * formula and not the import formula
Does anybody have any idea why this code doesn't work?
import turtle
test = turtle.Turtle()
test.color("orange")
test.pensize(5)
test.shape("turtle")
test.forward(100)
I use python 3.8
You've made a common error that I happended to also make when I was investigating your question.
I assume you have your code written in a file called 'turtle.py'? When you import turtle, it imports your file, not the turtle library.
Rename your file to something other than turtle.py, and your code should run fine.
Here is the result when I renamed my file from turtle.py to turtle2.py.
You need to end the loop that draws on the screen, by typing :
turtle.done()
This is my first time asking a question so please excuse me.
I am following a tutorial and using the code in this folder: https://github.com/ehmatthes/pcc_2e/tree/master/chapter_12/adding_ship_image
I literally replicated this folder in vs code with all the code and the names being used correctly.
For some reason I am getting this error:
Traceback (most recent call last):
File "/Users/sammyawad/Documents/projects/alieninvasion/alien_invasion.py", line 6, in <module>
from ship import Ship
ImportError: cannot import name 'Ship' from 'ship' (/Users/sammyawad/Documents/projects/alieninvasion/ship.py)
Also, I think I have an issue with linting because it is highlighting the pygame init functions for the class even though it works.
Change it to:
from .ship import Ship
import module as instance of class like this...
from ship import Ship as s1
I have a segmenting.py module in a package called processing.
I am trying to call a function in the module in my main. It is extremely simple.
In main.py
from processing import segmenting
segmenting.test()
In segmenting.py
def test():
print 'succeed'
However, I end up with errors as follows:
>>> from processing import segmenting
>>>
>>> segmenting.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'test'
>>>
Where went wrong?
The most likely cause is that you didn't restart your interactive interpreter after editing (and saving!) segmenting.py. Modules are imported only once and cached. If you edit the source code and then run the import statement again, the module is simply retrieved from the cache and doesn't pick up your changes. See also the reload() built-in.