Python中的BASE_DIR未定义

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

我正在尝试运行一个叫做 local_settings-example.py 的模块,这个模块我是在 这里 找到的。

这个模块位于 C:\Python27\pysec-master 文件夹里,下面是它的内容:

import os

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

但是每次我尝试运行它时,总是出现一些奇怪的 错误。每当我运行它,就会出现这个:

Traceback (most recent call last):
  File "<pyshell#10>", line 4, in <module>
    'NAME': os.path.join(BASE_DIR, 'db_name.sqlite3')
NameError: name 'BASE_DIR' is not defined

我该如何解决这个问题呢?最重要的是,BASE_DIR 能否在同一个 文件(C:\Python27\pysec-master) 中的另一个模块里定义?

1 个回答

1

你需要定义一个叫做 BASE_DIR 的变量。很简单——否则Python就不知道这个变量是什么,你就会遇到错误。我不知道这个变量应该是什么,所以我就不帮你填了。

如果你想在另一个模块中使用这个变量,你需要先 import 这个文件:

import relevant_file

然后你可以这样来访问它:

relevant_fle.BASEDIR

撰写回答