python-Django:导入模块

我试图用一个Django应用程序中的覆盆子pi来控制一个LED。我编写了一个python脚本来设置LED的颜色,一切正常。但是当我从views.py调用脚本时,我得到了一个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(\'\')

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

traceback44444

1个回答

  1. 项目“lampe”的名称隐藏了python找到的lampe/static/lampe/scripts中的“lampe.py”,并在导入“lampe.py”模块之前导入它。请重命名lampe.py并重试。

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>