python ‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)] NameError: name ‘os‘ is not defined

过年了准备跳槽,现在python这么火,感觉学习一下可能会很有用,于是网上找教程,在搭建Diango+python的时候突然一脸懵逼,一步步按照网上说的来的,可是第一步就报错了

在这里插入图片描述

我们可以看到报错提示os未定义,翻了下配置文件确实上面没有定义这个os
但是使用的BASE_DIR确有定义,我也是刚学python,不清楚这个os具体代表什么,为什么没有,可能是版本不同?
在这里插入图片描述

我使用的python是3.9.2
网上教程使用的大部分还是3.6之前的
然后我查了下资料,说这里只是指定那个文件templates的目录而已
将os.path.join(BASE_DIR, ‘templates’)改为’templates’是一样的意思
于是我就删除了
删除了再运行就没报错了


20210224更新
今天在网上又逛了一圈
在一篇文章中看到了os为什么报错的原因

出错信息如下:
‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]
NameError: name ‘os’ is not defined
在项目下settings.py文件中“TEMPLATES”中,默认会增加一行“‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]”用于配置模板地址。
NameError: name ‘os’ is not defined 说明没有找到OS
解决方法:
1、在settings.py文件头部增导入OS
import os
2、不用增加导入,修改默认生成的“‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)]”,修改如下:
‘DIRS’: [Path(BASE_DIR, ‘templates’)]
原因说明:
3.4版本之后,建议pathlib代替os

针对3.4版本之后的更改我有个疑问,我看教程用的3.6.1的也没报错,不知道有没大佬解答下。

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))
07-12
def split_dataset(img_path, target_folder_path, output_path): filename = os.listdir(img_path) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') os.makedirs(out_images, exist_ok=True) out_images_train = os.path.join(out_images, 'training') os.makedirs(out_images_train, exist_ok=True) out_images_test = os.path.join(out_images, 'test') os.makedirs(out_images_test, exist_ok=True) out_annotations = os.path.join(output_path, 'annotations') os.makedirs(out_annotations, exist_ok=True) out_annotations_train = os.path.join(out_annotations, 'training') os.makedirs(out_annotations_train, exist_ok=True) out_annotations_test = os.path.join(out_annotations, 'test') os.makedirs(out_annotations_test, exist_ok=True) for i in train: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_train, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_train, annotations_name) shutil.copyfile(annotations_src, annotations_dst) for i in test: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_test, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_test, annotations_name) shutil.copyfile(annotations_src, annotations_dst)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值