从另一个python程序运行django projact

时间:2016-09-09 06:44:36

标签: android python django server kivy

无论如何都是从另一个python程序运行django项目而不使用subprocess或os.system。

我试图使用:

os.system("python manage.py runserver")

和:

subprocess.call("python", "manage.py", "runserver")

但我想在Android中运行kivy并且android没有python内置

django本地服务器正在用作webwiew的服务器端。

我找到了runpy模块,但它无法运行django。

我该怎么做?

编辑1:

我这样做就像Tomasz Jakub Rup说的那样但是没有工作并给出以下错误:

    Python 3.5.1 (default, Mar  3 2016, 09:29:07) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import django
>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'sample.settings'
>>> if hasattr(django, 'setup'):
...     django.setup()
... 
>>> from django.core.management import call_command
>>> call_command('runserver')
/usr/bin/python: can't find '__main__' module in ''

2 个答案:

答案 0 :(得分:0)

首先你必须初始化django

import os

import django

os.environ['DJANGO_SETTINGS_MODULE'] = 'testapp.settings'
if hasattr(django, 'setup'):
    django.setup()

from django.core.management import call_command

运行命令:

call_command('runserver')

答案 1 :(得分:0)

django的内置开发服务器最初很好但是您可能希望使用uwsgi或gunicorn为您的应用程序提供服务。它们很有效,也可以在后台启动时运行而不是启动shell而你必须手动运行命令。你可以在这里查看更多相关信息。

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html