0

On my Pi3 running Raspian, Apache server I have a Django App.

I'm trying to run a python file from Django.
If I SSH into my Pi and type "python test_data.py", it runs fine. I SSH in as user "pi"

test_data.py is just this.

output = "success!"
print(output)

urls.py

url(r'^mygame/$', views.my_game),

views.py file I have the following

from subprocess import PIPE, run

def my_game(request):
    command = ['sudo', 'python test_data.py']
    result = run(command, stdout=PIPE, stderr=PIPE, shell=True, universal_newlines=True)
    return render(request, 'rungame.html',{'data1':result})

When /mygame is called via the web browser, here is the result I get printed in rungame.html, so I know it calls test_data.py. It appears to be a permissions issue? I don't understand what the following means. Can someone advise if this is a permissions issue and how do I fix it?

CompletedProcess(args=['sudo', 'python mygame.py'], returncode=1, stdout='', stderr='usage: sudo -h | -K | -k | -V\
nusage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]\
nusage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user]\n [command]\
nusage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p\n prompt] [-u user] [VAR=value] [-i|-s] [<command>]\
nusage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p\n prompt] [-u user] file ...\n')

Thank you

ADDED INFO: mygame.py created to test the concept

import pygame
import sys

pygame.init()

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))
background = pygame.image.load('background1.png')


print("test")
game_over = False

while not game_over:
    screen.blit(background, (0, 0))
    pygame.display.update()
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            sys.exit()

HERE IS MY APACHE CONF FILE

<VirtualHost *:80>
    ServerName www.example.com

    ServerAdmin webmaster@localhost

    Alias /static /home/pi/Dev/ehome/src/static
        <Directory /home/pi/Dev/ehome/src/static>
           Require all granted
         </Directory>

    <Directory /home/pi/Dev/ehome/src/ehome>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess ehome python-path=/home/pi/Dev/ehome/src:/home/pi/Dev/ehome/lib/python3.5/site-packages
    WSGIProcessGroup ehome
    WSGIScriptAlias / /home/pi/Dev/ehome/src/ehome/wsgi.py


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
8
  • the user pie needs to have permission to execute the python script. You are trying to execute the django application through apache right? Commented Jan 4, 2020 at 17:28
  • yes, I'm using django on apache. Commented Jan 4, 2020 at 17:56
  • when I run ls -l it shows Pi has permission on mygame.py however root is the owner of mygame.pyc Commented Jan 4, 2020 at 18:06
  • you also have to make apache's user (www:data) as the group owner and give it permission to execute the wsgi.py inside your application. I think you need to use something like modwsgi as well. For more info you may look at docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi Commented Jan 4, 2020 at 20:43
  • I'm struggling to get this, I have been reading for hours trying to find an answer. My Django app works, I can log in a user, read the database, navigate several different pages. So with that said, isn't apache's www-data user already using the wsgi.py file? Commented Jan 5, 2020 at 1:07

1 Answer 1

1

Don't use subprocess to call the script. You have a python script, make it a function and import it in Django. Then call the function in your views.py.

test_data.py

def my_function():
    output = "success!"
    return output

views.py

from test_data import my_function

def my_game(request):
    result = my_function()
    return render(request, 'rungame.html',{'data1':result})

Have the test_data.py script in the same directory with views.py.

Sign up to request clarification or add additional context in comments.

4 Comments

I understand what you're saying regarding a function. I created the test_data to figure out how to call another script. I need to be able to just call/run the test_data.py (and not just a function) because my ultimate goal is to use Django as menu system for several pygame apps. I have a mygame.py that opens a pygame window. In testing I can call "sudo python mygame.py" via SSH and the window opens on the screen connected to the Pi. I just can't get the same functionality from Django and it appears to be a permissions issue that I just don't understand. mygame.py code added above
I suggest you rename your question (+ title) to reflect what you are trying to achieve, as the original does not make sense. Make sure you mention execution of pygame script via Django.
My experience with pygame is limited, but it doesn't act as typical python module which you import. It is best somebody with pygame experience could advise you on best ways forward, but you need to correctly word your questions to get the help you need.
I don't think its related to pygame. this is why i created the little test_data to see if I was getting the same error or if it was caused the the set_mode or other function of pygame..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.