未定义Python名称“os”

2024-06-02 06:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

你知道我要做什么才能成功地运行这个模块吗?


Tags: 模块pathnamedebugprojectdbbasesettings
2条回答

只需添加:

import os

开始时,在:

from settings import PROJECT_ROOT

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

问题是你忘了导入操作系统。添加这行代码:

import os

一切都会好起来的。 希望这有帮助!

相关问题 更多 >