How do I fix the attribute error in python? - python

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

Related

Import function from a cython python module not working

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

Error with importing a class from a module

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

How can I solve the error "Cannot import name 'x' "?

I'm trying to convert Keras to a Core ML model but I'm stuck when converting the Python file into a mlmodel.
I'm getting errors when importing submodules of 'coremltools'.
The error that I'm getting is: "python recog.py
Traceback (most recent call last):
File "recog.py", line 3, in
from coremltools import convert
ImportError: cannot import name 'convert'
"
I tried to import the submodules in a different way but nothing worked for me.
I hope anyone can help me!
You can see the Python code, in the sample below:
import coremltools
from coremltools import converters
from coremltools import convert
coreml_model = coremltools.converters.keras.convert('model.h5', input_names='data', image_input_names='data', is_bgr=True, output_names='species')
coreml_model.save('model.mlmodel')
Make sure the name you are trying to import is in the module coremltools.
In the file, coremtools.py double check if the name is same I.e. convert.
Check the location of the coremtools.py file, is it in the main folder of python where python.exe exists?

opencv2 Aruco library modules not working with python

I have compiled the aruco library as stated here github link for aurco library
I have checked it has compiled successfully as i can import it in python without any error and to check i have run the example.py script also it's working but when i wrote this code
import cv2
import numpy as np
import aruco
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
aruco.drawMarker(Dictionary,5,250,markerImage,1)
aruco.drawMarker(Dictionary,10,250,markerImage,1)
aruco.drawMarker(Dictionary,20,250,markerImage,1)
aruco.drawMarker(Dictionary,25,250,markerImage,1)
aruco.drawMarker(Dictionary,50,250,markerImage,1)
aruco.drawMarker(Dictionary,100,250,markerImage,1)
aruco.drawMarker(Dictionary,200,250,markerImage,1)
cv2.imshow("markers",markerImage)
cv2.waitKey(0)
cv2.imgwrite(marker.jpg,markerImage)
it throws error
Traceback (most recent call last): File "drawmarker.py", line 7, in
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
AttributeError: 'module' object has no attribute
'getPredefinedDictionary'
can someone please let me know what am i doing wrong, is this module not imported in python version of aruco ?
maybe you should try this "aruco.DICT_5X5_250" as parameter, like...
dict = aruco.getPredefinedDictionary( aruco.DICT_5X5_250 )
it worked for me :)

Turtle Module in python not importing

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()

Categories

Resources