【问题标题】:Python GUI wont import + SQlite ENGINEPython GUI 不会导入 + SQlite ENGINE
【发布时间】:2014-05-27 16:18:19
【问题描述】:

作为 Python 初学者,我必须理解这段代码:

from settings import PROJECT_ROOT

--> 我正在尝试在 Python Shell 中输入此内容,但即使我有这样的模块,Python 也会给我一个 Traceback

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

--> 我想使用 Python 内置的数据库 sqlite,但我真的不明白我必须做什么

请原谅我的问题很简单,但我对这些天在 Python 中的任务感到不知所措。

为了完整起见,这是模块中的所有代码,称为settings.py

from settings import PROJECT_ROOT

DEBUG = True
TEMPLATE_DEBUG = DEBUG


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}


# Make this unique, and don't share it with anybody.
SECRET_KEY = ''

# Python dotted path to the WSGI application used by Django's runserver; added in v1.4
WSGI_APPLICATION = 'wsgi.application'

############### PYSEC specific variables

# assumes this directory exists
DATA_DIR = "%s/pysec/data/" % PROJECT_ROOT

更新

我不想强调你已经过度紧张的耐心,但为什么它一直告诉我SECRET_KEY 值为空?我把

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'sdfgtardyure34654356435'

它给了我文字

raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

这是cmd的图片

【问题讨论】:

  • 您发布的文件是名为settings.py 的文件吗?如果是这样,您不会导入到 same 文件中,而是导入到不同的文件中。
  • 是的,文件名为settings.py
  • 我尝试在 GUI 中使用此命令,但它不起作用
  • 您有两个问题: 1. 您没有使用import 在同一个文件中查找内容。您可以使用它在 another 文件中查找某些内容。 2. 另外,在您的settings.py 中有注释,无论如何都会创建一个名为PROJECT_ROOT 的值。
  • 可能在另一个模块中吗?创建PROJECT_ROOT的那个?

标签: python django settings python-import


【解决方案1】:

尝试使用python manage.py shell打开python shell。

通常settings.py文件位于项目根目录中,所以要导入PROJECT_ROOT变量,可以使用from project_name.settings import PROJECT_ROOT

[编辑]

要使用 sqlite 引擎,请将 DATABASES 字典更改为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, '/project_database/db_name.sqlite3'),
    }
}

2[编辑]

没有压力。作为提示,请参阅此Adding Python Path on Windows 7 问题以将 python 文件添加到 win 路径变量,这有助于避免将项目放在 c:PythonXX 中,而是使用另一个目录。

我查看了链接的 github 项目,它似乎在 README 文件中解释了您必须添加一个 SECRET_KEY 和一个 DATA_DIR 变量。

这是我为完成该项目而采取的解决方法:

$ git clone https://github.com/lukerosiak/pysec.git
$ cd pysec
$ ls # the dir command when on Windows
README.md
TODO.md
local_settings-example.py
manage.py*
pysec/
requirements.txt
settings.py*
$ cp local_settings-example.py local_settings.py

编辑local_settings.py文件并修改SECRET_KEY和DATA_DIR变量:

SECRET_KEY = '@r65u-33&#2v3vu-e^h-%u4kg=g9y5z'
DATA_DIR = '/home/slacker/pysec/project_database' # or something like: C:\\users\tosh\pysec\

project_database 运行:

$ python manage.py syncdb

希望对你有帮助!

【讨论】:

  • 我在命令提示符下输入了 python manage.py shell' and it didn't work. I also tried to type the same thing in the GUI but it immediately gave me invalid syntax. The settings.py` 在 Python27 文件中...我还尝试在 GUI 和 CMD 中输入 from project_name.settings import PROJECT_ROOT 但什么也没有。嗅探
  • 在执行python manage.py shell 之前,您必须切换到项目根目录(因为manage.py 就在那里)。例如,我的项目位于/home/user_my/project_name 中,所以去那里cd /home/user_my/project_name 然后执行python manage.py shell
  • 它说SECRET_KEY 不能为空,尽管我填了 10 个字符 SECRET_KEY 关闭了 cmd 并重新打开它并转到了 roor 文件。
  • 这是BAZINGAAAAAAAAAAAA
  • 感谢@sgmart 的输入!!!但是,每当我尝试在 cmd 中键入 $ git clone https://github.com/lukerosiak/pysec.git 时,我都会遇到一个小问题,它说 git 未被识别为内部或外部命令
猜你喜欢
  • 2018-02-05
  • 2015-09-23
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多