This question already has answers here:
NameError: name 'datetime' is not defined

(2个答案)


2年前关闭。




我正在尝试运行此python模块
from settings import PROJECT_ROOT

DEBUG = True
TEMPLATE_DEBUG = DEBUG


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


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

# 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

但是每当我尝试通过F5运行它时,我都会得到这个
Traceback (most recent call last):
  File "C:\Python27\pysec-master\local_settings-example.py", line 11, in <module>
    'NAME' : os.path.join(BASE_DIR, 'db_name.sqlite3'),
NameError: name 'os' is not defined

该模块位于C:\Python27\pysec-master中,我为here得到了pysec

您知道我该怎么做才能成功运行该模块吗?

最佳答案

只需添加:

import os

在开始之前:
from settings import PROJECT_ROOT

这将导入python的模块 os ,显然,该模块稍后将在模块代码中使用而不被导入。

08-27 09:20