ImportError: Failed to import settings. Is it on the way?

I get an import error when I try to run a script in my Django application.

This is due to the settings file.

Error:

File "bookd/get_data.py", line 10, in <module> from models import UserProfile File "/home/hiccup/DataProjects/goodread/bookda/bookd/models.py", line 3, in <module> from django.db import models File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 14, in <module> if not settings.DATABASES: File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 276, in __getattr__ self._setup() File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup self._wrapped = Settings(settings_module) File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 89, in __init__ raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)) ImportError: Could not import settings 'bookda.settings' (Is it on sys.path?): No module named bookda.settings 

I know that this is a very simple error, and there are answers to its solution, but I just can not solve it, no matter how I try to add the path to the settings. And it's pretty frustrating that other django applications on my system work just fine.

Hierarchy:

 bookda/settings.py bookda/books/script.py 

I get an error when running script.py.

+4
source share
2 answers

If you are trying to write a custom script that uses your Django [s] model, you need to make sure that Django can find your project package. Put this at the top of your get_data.py script ...

 import sys sys.path.append('/home/hiccup/DataProjects/goodread') 

... then he should be able to import the bookda module.

You may also need to tell Django where the settings file is located, in which case you will need ...

 import sys import os sys.path.append('/home/hiccup/DataProjects/goodread') os.environ['DJANGO_SETTINGS_MODULE'] = 'bookda.settings' # Now we can import Django stuff from models import UserProfile ... 
+6
source

Perhaps your settings.py file has a syntax error or another error, which means that it cannot be executed. In some cases, this leads to import errors.

Put a import pdb;pdb.set_trace() first in the file and you can debug it. If it is not included in the debugger, then Python really did not find the file.

+3
source

Source: https://habr.com/ru/post/1479222/


All Articles