Error: name 'os' is not defined

Answered

Hello!

Look. I use the Django for first time. So, I've got a problem.

I'm trying to make command  "python manage.py runserver".

And it says: Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\core\management\__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\conf\__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\dr5se\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\dr5se\PycharmProjects\djangoProject\djangoProject\settings.py", line 57, in <module>
'DIRS': [os.path.join(BASE_DIR, 'templates')]


NameError: name 'os' is not defined

 

What it might be?.. How to solve?

P.s. Can I write in russian here?))))) Or only english?

12 comments
Comment actions Permalink

Unfortunately, it doesn't work...

 

And one thing else: I have even tree such files....

Which of these files should be changed?..

Should I undo changes, made for these files?

0
Comment actions Permalink

You need to add the import statement to the file Python in complaining about. ~\PycharmProjects\djangoProject\djangoProject\settings.py

 

>Should I undo changes, made for these files?

Yes.

0
Comment actions Permalink

It's still doesn't work..

Now it's new problem:

File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\dr5se\PycharmProjects\djangoProject\venv\lib\site-packages\django\conf\__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\dr5se\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\dr5se\PycharmProjects\djangoProject\djangoProject\settings.py", line 81, in <module>
'NAME': BASE_DIR / 'db.sqlite3',


TypeError: unsupported operand type(s) for /: 'str' and 'str'

 

0
Comment actions Permalink

This one doesn't seem to be related to PyCharm.
Did you create this project from PyCharm or from CMD?

If you replace BASE_DIR with this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

does it help?

0
Comment actions Permalink

But it's PyCharm..  https://yadi.sk/i/n0oAvI4Hnz9asw       

BASE_DIR  has been edited..

And I'm not sure, if I discarded changes in other files "settings.py" correctly..

By the way, should I activate the installation option "add launchers dir to the PATH"? Would it replace the older settings?

0
Comment actions Permalink

>But it's PyCharm..  https://yadi.sk/i/n0oAvI4Hnz9asw    

Yes, you run it in PyCharm's terminal. But the terminal in PyCharm is basically your system terminal, which means that if you run this command in the system terminal from the same venv, you will get the same error.

Could you create a new project, then add the missing import os statement (or use older Django version to avoid that) and let me know if the problem reproduces?

0
Comment actions Permalink

May be, I doing something wrong, but it doesn't work.

I reinstalled PyCharm, created new project, added missing statement to settings.py, replaced BASE_DIR.......

But no. Old error " File "C:\Users\dr5se\PycharmProjects\djangoProjectNew\djangoProjectNew\settings.py", line 80, in <module>
'NAME': BASE_DIR / 'db.sqlite3',
TypeError: unsupported operand type(s) for /: 'str' and 'str'
"

I must misunderstood something.

Apologies from a novice user.

0
Comment actions Permalink

Ahh, I see, so in this case, you don't need to replace BASE_DIR.
Just adding import os should be enough.

So it should remain

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
1
Comment actions Permalink

I have the EXACT SAME problem.    I get this right from the moment I start a brand new project in Pycharm Professional...  I am unclear what needs to be done to resolve and why is happens at all???   This is a fresh problem. 

 

Traceback (most recent call last):
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/manage.py", line 22, in <module>
main()
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/simontucker/PycharmProjects/djangoProject_ETH_API/djangoProject_ETH_API/settings.py", line 58, in <module>
'DIRS': [os.path.join(BASE_DIR, 'templates')]
NameError: name 'os' is not defined

0
Comment actions Permalink

@Simon Tucker

You need to add import os to /Users/simontucker/PycharmProjects/djangoProject_ETH_API/djangoProject_ETH_API/settings.py file.

0
Comment actions Permalink

To Sergey Karpov:  Yesssssss!!!!!!!!! It work's!!! Exactly right advice! Precisely without replacing:

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent

0

Please sign in to leave a comment.