没有定义 Python 名称‘ os’

我正在尝试运行这个 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中,我得到了 给你的 pysec

你知道要成功运行这个模块我必须做什么吗?

276004 次浏览

问题是您忘了导入 os:

import os

一切都会好起来的。 希望这个能帮上忙!

只要加上:

import os

在开始,在:

from settings import PROJECT_ROOT

这将导入 python 的模块 奥斯,该模块显然将在以后的模块代码中使用,而不会被导入。