Python名称'os'未定义

81 投票
2 回答
328250 浏览
提问于 2025-04-18 02:36

我正在尝试运行这个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

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

2 个回答

36

问题是你忘记导入os模块了。你只需要加上这一行代码:

import os

这样就没问题了。希望这能帮到你!

173

只需要在开头加上:

import os

放在下面这段代码之前:

from settings import PROJECT_ROOT

这样做会导入Python的一个模块os,这个模块在你代码的后面部分会用到,但之前并没有导入。

撰写回答