Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从shell获取django.apps,但不从脚本获取_Python_Django_Bash_Shell_Django Settings - Fatal编程技术网

Python 从shell获取django.apps,但不从脚本获取

Python 从shell获取django.apps,但不从脚本获取,python,django,bash,shell,django-settings,Python,Django,Bash,Shell,Django Settings,我可以在shell中获取我的应用程序信息: In [1]: from django.apps import apps In [2]: info = apps.all_models In [3]: for item in info: print(item) ...: mailer auth analytics subscribe staticfiles contenttypes layout contact 但不是在脚本中。脚本位于主

我可以在shell中获取我的应用程序信息:

In [1]: from django.apps import apps

In [2]: info = apps.all_models      

In [3]: for item in info:           
    print(item)
   ...:     
mailer
auth
analytics
subscribe
staticfiles
contenttypes
layout
contact
但不是在脚本中。脚本位于主站点文件夹中,即带有
manage.py
,而设置文件位于
mysite/project/settings.py
。脚本就在项目文件夹上方的mysite中:

#!/usr/bin/env python
# coding: utf-8

import os, sys, subprocess, time, re, ast
# This should be the directory that contains the directory containing settings.py
# sys.path.add('/path/to/mysite') 
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

HOMEPATH = os.path.expanduser("~")

PROJECT_PATH = "{}/work_projects/mysite".format(HOMEPATH)

ENTRY_PATH = os.path.join(PROJECT_PATH, "project")

MOMMY_PATH = os.path.join(PROJECT_PATH, "model_mommy")

MOMMY_TESTPATH = os.path.join(MOMMY_PATH, "tests")

sys.path.append(ENTRY_PATH) 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.project.settings")

from django.apps import apps

info = apps.all_models

for item in info:           
    print(item)

没有输出,信息为空。导入
django.conf.settings
并使用这些设置确实有效。为什么在这个脚本中做同样的事情不起作用?谢谢你

你可能错过了这个(我认为mysite不应该在设置行中):


您可能错过了这一点(我认为mysite不应该在设置行中):

import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import django
django.setup()

for item in django.apps.all_models:
    print(item)