AttributeError: 'function' object has no attribute 'make' in Flask web app - python

I am trying to generate QR code in python. My code is working fine in another python file but throws an error when I tried to use it in Flask webapp.
I've already installed pillow and qrcode.
#app.route('/qr')
def qrcode():
img = qrcode.make('https://youtube.com')
img.save('first-image1.png')
return 'all good'

Inside your function qrcode() refers to the local function, not to the external qrcode module/function/object. Your qrcode() function does not have any attribute named make, therefore an exception is raised. Here's an example:
def f():
print(f.make)
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
AttributeError: 'function' object has no attribute 'make'
To fix this, rename your function so that it does not clash with qrcode:
#app.route('/qr')
def qr():
img = qrcode.make('https://youtube.com')
img.save('first-image1.png')
return 'all good'
Here I simply renamed your function to qr() so that it will no longer shadow qrcode.

Related

AttributeError: 'NoneType' object has no attribute 'storid'

I am really new with ontologies especially with owlready2. I loaded an Ontology the basic example Pizza and imported I think successfully on python (I checked whether I can see the classes which I can so..)
Than I used the following code to search one class specifically with the method search():
from owlready2 import *
onto_path.append(r"C:/Users/AyselenKuru/Desktop/owl_docs/owlpizza.owl")
onto=get_ontology(r"C:/Users/AyselenKuru/Desktop/owl_docs/owlpizza.owl")
onto.load()
am= onto.search_one(is_a= onto.American)
for x in onto.classes():
print(x)
I want to know how can I search/get one specific Class and an Attribute and I get the following error message:
Traceback (most recent call last):
File "c:\Users\AyselenKuru\Desktop\pizza_ex1.py", line 6, in <module>
am= onto.search_one(is_a = onto.American)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AyselenKuru\AppData\Local\Programs\Python\Python311\Lib\site-packages\owlready2\namespace.py", line 395, in search_one
def search_one(self, **kargs): return self.search(**kargs).first()
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\AyselenKuru\AppData\Local\Programs\Python\Python311\Lib\site-packages\owlready2\namespace.py", line 364, in search
else: v2 = v.storid
^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'storid'
Problem solved itself, the example owl File had errors with IRI that caused a Problem with the search

Python Error: TypeError: 'module' object is not callable

I have written the simplest python code (module) :
def newplus(x, y):
return x*y
This is stored in a folder sabya (which is my package). The folder has _init_ and newplus.py files.
In my IDLE I can open the module sabya.newplus. When I give import sabya.newplus there is no error. but when I issue :
>>> sabya.newplus(2, 3)
I am getting this error
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
sabya.newplus(2, 3)
TypeError: 'module' object is not callable
You need to qualify the function as follow:
sabya.newplus.newplus(2, 3)

Has no attribute getitem

I'm trying to integrate Pagseguro (a brazilian payment service, similar to PayPal) with this lib
https://github.com/rochacbruno/python-pagseguro
But, I don't know how to access the data from notification that the service sends to me. This is my code:
notification_code = request.POST['notificationCode']
pg = PagSeguro(email="testPerson#gmail.com", token="token")
notification_data = pg.check_notification(notification_code)
print notification_data['status']
In the las line I receive this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'PagSeguroNotificationResponse' object has no attribute '__getitem__'
The documentation in the README doesn't seem to match the code. It looks like rather than notication_data being a dictionary it is an object that has attributes matching the dictionary keys from the README.
So this should work if you just change print notification_data['status'] to the following:
print notification_data.status

AttributeError: 'module' object has no attribute 'TextCalendar'

When I copy the below code in Ideone then it runs fine but it is not running in the text editor and showing the error mentioned in the subject of this post:
import calendar
c = calendar.TextCalendar(calendar.SUNDAY)
c.prmonth(2007, 7)
The complete error is:
Traceback (most recent call last):
File "calendar.py", line 1, in <module>
import calendar
File "/home/shl/Desktop/calendar.py", line 2, in <module>
c = calendar.TextCalendar(calendar.SUNDAY)
AttributeError: 'module' object has no attribute 'TextCalendar'
change the program name from calendar.py to something like calendar_code.py
This wold work properly.
Importing from builtin library when module with same name exists

Python help() function and the string.title function

Why doesn't
import string;help(string.title)
seem to work but
help(string.strip)
works just fine?
I get the error
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no
attribute 'title'
title is a method on objects of type str, not a function in the string module. That means you can do "foo".title() or str.title("foo") but not string.title("foo").
help(str.title) seems to work just fine.

Categories

Resources