簡體   English   中英

如何從Django管理命令執行python腳本?

[英]How to execute a python script from django management commands?

我正在嘗試執行不在Django Project目錄中的python腳本...例如,這是我使用以下命令調用的管理類:

我需要以ROOT身份運行它,因為它需要訪問我的樹莓派上的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()

也許我正在以錯誤的方式進行此操作,我只是在學習將Web框架與樹莓派相結合。

在您的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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM