编程语言
首页 > 编程语言> > python-DJANGO_SETTINGS_MODULE如何配置

python-DJANGO_SETTINGS_MODULE如何配置

作者:互联网

我正在使用Django 1.8和Python-3.4的项目中
我想安装模型包,以在应用程序中自动创建数据.
我已经使用pip install django-mockups和easy_install django-mockups安装了此软件包.

我在我的settings.py文件的INSTALLED_APPS中添加了“ mockups”条目

INSTALLED_APPS = (
    'suit',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mockups',
    'tracks',
    'albums',
    'artists', 
    'userprofiles',
)

我想在控制台内的django admin实用程序中看到与安装的软件包有关的可用命令,但是最后,我收到了有关环境变量DJANGO_SETTINGS_MODULE的注释消息,而我无法在django-mockups软件包中看到清单

(venv)➜  myproject  django-admin help 

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    runfcgi
    runserver
    shell
    showmigrations
    sql
    sqlall
    sqlclear
    sqlcustom
    sqldropindexes
    sqlflush
    sqlindexes
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    syncdb
    test
    testserver
    validate
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

(venv)➜myproject

我检查django-mockups软件包是否已安装,并检查以下路径目录是否存在.

 /home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django_mockups-0.4.8.dist-info` and
 /home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/mockups`

并安装了django-mockups软件包

(venv)➜  mockups  pip freeze
Django==1.8.2
django-mockups==0.4.8
django-suit==0.2.13
Pillow==2.9.0
wheel==0.24.0
(venv)➜  mockups 

我的DJANGO_SETTINGS_MODULE是这样设置的:
在manage.py文件中

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sfotipy.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

并在wsgi.py中

"""
WSGI config for myproject project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

application = get_wsgi_application()

最后,当我尝试启动django服务器时,得到以下输出:

(venv)➜  myproject  ./manage.py runserver 
/home/bgarcial/.virtualenvs/venv/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  return f(*args, **kwds)

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/django/apps/config.py", line 86, in create
    module = import_module(entry)
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/mockups/__init__.py", line 2, in <module>
    from mockups.factory import Factory
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/mockups/factory.py", line 1, in <module>
    from mockups import generators
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/mockups/generators.py", line 100, in <module>
    class StringGenerator(Generator):
  File "/home/bgarcial/.virtualenvs/venv/lib/python3.4/site-packages/mockups/generators.py", line 101, in StringGenerator
    coerce_type = unicode
NameError: name 'unicode' is not defined
(venv)➜  myproject 

如何正确设置我的DJANGO_SETTINGS_MODULE环境变量?
这是DJANGO_SETTINGS_MODULE配置,因此该模型不起作用吗?
坦克很多:)

谢谢

解决方法:

我在github中找到了这个issue.

也许django-mockups不支持Python 3.

Python 3将unicode更改为str,将旧的str更改为字节.

因此,如果您使用Python 3运行django-mockups.将引发NameError

如回溯所示,django-mockups是用Python 2编写的.

标签:django-settings,python,django
来源: https://codeday.me/bug/20191120/2041025.html