DEV Community

Will Vincent for Learn Django

Posted on • Originally published at learndjango.com

Django NameError: name 'os' is not defined

If you've started a new Django 3.1+ project and are using older tutorials or guides, it's likely to come across the following error on your command line:

NameError: name 'os' is not defined
Enter fullscreen mode Exit fullscreen mode

Starting with Django 3.1, the startproject command generates a settings.py file that imports pathlib rather than os on the top line.

The quick fix is to import os at the top of your settings.py file:

# settings.py
import os # new
from pathlib import Path
Enter fullscreen mode Exit fullscreen mode

The better fix is learn more about how pathlib works and update your BASE_DIR, DATABASES, STATICFILES_DIRS, and other files to use the newer, modern approach.

Top comments (2)

Collapse
 
micheisalwayson profile image
micheisalwayson

thanks

Collapse
 
laurrvt profile image
Laur.Rvt

thanks) . I'm new in Django ,so I thought the problem fix will be harder