Reputation: 77
I'm trying to control a LED with a raspberry pi from a Django application. I wrote a python script to set the color of the LED and everything is okey. However when I call the script from my views.py I got an AttributeError : module has no attribute.
Views.py
import sys, os
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(PROJECT_DIR, 'static/lampe/scripts'))
import launcher, lampe
"""Vue utilisee pour appliquer une couleur"""
class Appliquer_couleur(View):
def get(self, context, **reponse_kwargs):
print(self.kwargs['pk'])
couleur_serialized = CouleurSerializer(Couleur.objects.get(pk=self.kwargs['pk']))
launcher.launch(couleur_serialized.data['code'])
return HttpResponse('')
In the directory "lampe/static/lampe/scripts" I have three files : an empty init.py, lampe.py and launcher.py
Lampe.py
import sys
import RPi.GPIO as GPIO
def setCouleur(arg1):
..
Launcher.py
import sys, os
from multiprocessing import Process
import lampe as LED
def launch(arg1):
try:
process = Process(target=LED.setCouleur, args=(arg1,))
process.start()
process.join()
except KeyboardInterrupt:
pass
if __name__== "__main__":
sys.exit(launch(sys.argv[1]))
Traceback
Internal Server Error: /lampe/appliquer_couleur/1/
Traceback (most recent call last): File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/pyrotecnix/Projet/aurore/lampe/views.py", line 85, in get
launcher.launch(couleur_serialized.data['code'])
File "/home/pyrotecnix/Projet/aurore/lampe/static/lampe/scripts/launcher.py", line 9, in launch
process = Process(target=LED.setCouleur, args=(arg1,))
AttributeError: module 'lampe' has no attribute 'setCouleur'
[16/May/2016 17:29:00] "GET /lampe/appliquer_couleur/1/ HTTP/1.1" 500 69516
Upvotes: 1
Views: 1862
Reputation: 518
the name of your project "lampe" is concealing the "lampe.py" in lampe/static/lampe/scripts as it is being found by python and importing it before importing the 'lampe.py' module. please rename your lampe.py and try again.
Upvotes: 3