如何从django管理命令执行python脚本?

时间:2016-05-25 03:42:32

标签: python django raspberry-pi execfile

我尝试执行一个不在我的Django Project目录中的python脚本...例如,这是我用命令调用的管理类:

我需要将其作为ROOT运行,因为它需要访问我的覆盆子pi上的GPIO引脚。 (我得到另一个错误)

(env) sudo python manage.py bubbles

调用此脚本:execfile(' /home/pi/rpi/bubbles.py')

# /home/pi/DjangoProjects/RaspberryPi/graphics/management/commands

from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from graphics.models import Graphic


class Command(BaseCommand):

    help = "My test command"

    def handle(self, *args, **options):

        execfile('/home/pi/rpi/bubbles.py')

我收到此错误

Traceback (most recent call last):
   File "manage.py", line 8, in <module>
 from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

所以我猜测这是我的虚拟环境的问题,是否有办法在虚拟环境范围之外执行脚本?有没有更好的方法来使用django命令或其他方法执行此脚本。我有没有意义。

python脚本我试图打电话:

#!/usr/bin/env python

import Image
import ImageDraw
import time
from rgbmatrix import Adafruit_RGBmatrix

# Rows and chain length are both required parameters:
matrix = Adafruit_RGBmatrix(16, 2)

# Bitmap example w/graphics prims
image = Image.new("1", (16, 64)) # Can be larger than matrix if wanted!!
draw  = ImageDraw.Draw(image)    # Declare Draw instance before prims


# 24-bit RGB scrolling example.
# The adafruit.png image has a couple columns of black pixels at
# the right edge, so erasing after the scrolled image isn't necessary.
while True:

    matrix.Clear()
    image = Image.open("bubbles.png")
    image.load()
    for n in range(64, -image.size[0], -1):
        matrix.SetImage(image.im.id, n, 1)
        time.sleep(0.025)

    matrix.Clear()

也许我以错误的方式解决这个问题,我只是学习将网络框架与我的树莓派合并。

1 个答案:

答案 0 :(得分:0)

bubbles.py 中,而不是执行此操作

def handle(self, *args, **options):

    execfile('/home/pi/rpi/bubbles.py')

试试这个

def handle(self, *args, **options):

    execfile('PATH_TO_YOUR_ENV/bin/python /home/pi/rpi/bubbles.py')