Django:导入一个模块

时间:2016-05-16 15:16:04

标签: python django import

我正在尝试使用Django应用程序中的覆盆子pi控制LED。 我写了一个python脚本来设置LED的颜色,一切都很好。 但是,当我从views.py调用脚本时,我得到了一个AttributeError:模块没有属性。

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

在目录“lampe / static / lampe / scripts”中我有三个文件: 一个空的 init .py,lampe.py和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]))

回溯

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

1 个答案:

答案 0 :(得分:3)

您的项目名称“lampe”隐藏了lampe / static / lampe / scripts中的“lampe.py”,因为它是由python发现并在导入'lampe.py'模块之前导入它。请重命名你的lampe.py并重试。