未定义 Python 名称“os”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23048129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:10:51  来源:igfitidea点击:

Python name 'os' is not defined

pythonimport

提问by ExoticBirdsMerchant

I am trying to run this python module

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

But whenever i try to run it by F5i get this

但是每当我尝试运行它时,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

The module lives in the C:\Python27\pysec-masterand i got pysec for here

该模块位于C:\Python27\pysec-master,我在这里得到了 pysec

Do you know what must i do to run the module with success?

你知道我必须做什么才能成功运行模块吗?

采纳答案by Ivaylo

Just add:

只需添加:

import os

in the beginning, before:

一开始,之前:

from settings import PROJECT_ROOT

This will import the python's module os, which apparently is used later in the code of your module without being imported.

这将导入 python 的模块os,它显然稍后在您的模块代码中使用而没有被导入。

回答by Quintec

The problem is that you forgot to import os. Add this line of code:

问题是你忘记导入 os. 添加这行代码:

import os

And everything should be fine. Hope this helps!

一切都应该没问题。希望这可以帮助!