Why I get error raise ImproperlyConfigured - django

I want to add info in database using django-seed.
seed.py
from django_seed import Seed
from models import Employee
seeder = Seed.seeder()
seeder.add_entity(Employee, 5)
inserted_pks = seeder.execute()
When I try to start command: python3 seed.py
I've got this error:
Traceback (most recent call last):
File "seed.py", line 2, in <module>
from models import Employee
File "/home/kaucap/test/myproject/app_worker/models.py", line 4, in <module>
class Employee(models.Model):
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/db/models/base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/apps/registry.py", line 137, in check_apps_ready
settings.INSTALLED_APPS
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/home/kaucap/test/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 67, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Can anyone explain why I have this error and how I can fix it?

python3 seed.py started python without Django settings.
if you are want to start seed.py in Django "environment",
in your django-pproject folder:
python3 manage.py shell
Django shell starts, and in shell:
import seed.py
If you see the same error - you have not created any Django project yet.

Related

Django Error "ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier."

I am trying to host a website using django and I installed fontawesome-free[version=5.15.3] which I used in my project but when I am trying to run python manage.py makemigrations I am getting this error ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier. How do I fix this problem?
Full traceback:
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 "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/config.py", line 255, in create
return app_config_class(app_name, app_module)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/config.py", line 38, in __init__
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier.
As #AKX said, this is a known issue for fontawesome-free, fontawesome-pro + django >= 3.2.
This quick-fix helped.
Just before the INSTALLED_APPS add this
import sys
sys.modules['fontawesome_free'] = __import__('fontawesome-free')
or
sys.modules['fontawesome_pro'] = __import__('fontawesome-pro')
and then add "fontawesome_free" or "fontawesome_pro" to your INSTALLED_APPS
INSTALLED_APPS = [
.........
"fontawesome_free",
#or
"fontawesome_pro",
.........
]
Semi fixed as of May 21st. It's fixed with renaming the package to fontawesomefree. Here is the github issue for more info
This is a known issue in the fontawesome-free Python package, rendering it unusable as a Django app for the time being: https://github.com/FortAwesome/Font-Awesome/issues/17801

Django 1.6: settings file not found no matter what

I searched deep and wide, but no solution has worked so far.
I'm trying this with Django 1.6 in my virtual environment. In my manage.py file I have the following line:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproj.settings")
With this, when I execute python ./manage.py runserver I get the traceback:
Traceback (most recent call last):
File "./manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/home/int/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/home/int/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/int/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
app_name = get_commands()[subcommand]
File "/home/int/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
File "/home/int/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/int/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/home/int/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myproj.settings' (Is it on sys.path?): cannot import name current_app
Then I added
sys.path.insert(0, '/home/int/Documents/myproj/myproj'), where the modules are
to my manage.py file and got the same traceback as above.
If I try relative import, i.e. changing that line in manage.py to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", ".myproj.settings")
I get the error
TypeError: relative imports require the 'package' argument
The file settings.py is definitely in /home/int/Documents/myproj/myproj (triple-checked).
So I don't know how to solve this seemingly simple issue. Would appreciate some help with it.

Problem with Django app unit tests under Visual Studio Code

I have a draft of the Django project with the added one application (e.g. my_app). In this app I have places tests.py file with one test:
import unittest
class Test_MyModel(unittest.TestCase):
def test_dummy(self):
self.assertEqual(1,1)
In this case, this dummy test is discoverd by Visual Studio Code and could be executed in it, also it's possible to launch this test from command line:
python manage.py test
When I modify my file with unit test add import some model (placed in file models.py) from my_app:
import unittest
from .models import MyModel # new added line
class Test_MyModel(unittest.TestCase):
def test_dummy(self):
self.assertEqual(1,1)
In this case I'm still be able run test from command line without any problems, but VSC doesn't discover my test and in the Python Test Log in the VSC I get error:
======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 434, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 375, in _get_module_from_name
__import__(name)
File "/Users/myuser/Projects/backend/my_app/tests.py", line 3, in <module>
from my_app.models import MyModel
File "/Users/myuser/Projects/backend/my_app/models.py", line 4, in <module>
class MyModel(models.Model):
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Any hints or advice how can I resolve this problem and start fully using unit test under VSC for models test?
I've found a solution to my problem. I need to add below lines into my init.py file in my_app folder.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Django runserver command not working

I'm trying to setup a local django installation, but am facing a lot of problems in configuring it properly.
Trying to start the server doesn't work:
admins-macbook:myproject Admin$ python manage.py runserver 8080
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 8, in <module>
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 26, in <module>
from django.views import static
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/views/static.py", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 48, in __getattr__
if settings.USE_I18N:
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/Users/Admin/myapp/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings '{{ project_name }}.settings' (Is it on sys.path?): No module named {{ project_name }}.settings
I have mainly been following the directions here.
I am completely lost about what I may be doing wrong. I googled and found some people saying that either this could be because of naming conflicts between the project name or trying to use two installations simultaneously. I changed the name of my project, and checked to see that I only have one django installation, but still am facing the same problem. Can anyone help? Thanks for any ideas!
EDIT: Checking the paths
admins-macbook:Downloads Admin$ echo $PATH
/Users/Admin/myapp/lib/python2.7/site-packages/django/conf/myproject:/Users/Admin/myapp/lib/python2.7/site-packages/django/conf:/Users/Admin/myapp/lib/python2.7/site-packages/django:/Users/Admin/myapp/lib/python2.7/site-packages:/Users/Admin/myapp/lib/python2.7:/Users/Admin/myapp/lib:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
EDIT: Adding the directory structure (did not change, straight from django 1.4.2):
-bin
-contrib
-core
-(etc.)
-django
--conf
---__init__
---app_template
---urls
---myproject
----myapp
-----settings.py
-----urls.py
-----(etc.)
----manage.py
When manage.py is first created it includes a code that adds the path to the project's settings.py to the python path whenever a command (such as run server) is run. Since you changed the names of the folders, manage.py can't find the settings anymore.
Open up manage.py and change the old project name there to the new one. There are other places where this problem might show up so keep an eye for references to your old project's name that would cause trouble

Google App engine, django, userena, database setup

I am trying to build a web app with GAE, Django. The user registration would be userena. I already use the django-nonrel and all the setup to get the first part working and I am trying to syncdb for userena. I am keep getting this errors, Could you please help me on this.
BB8265:epapyri-django$ sudo django-admin.py syncdb
Password:
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Library/Python/2.7/site-packages/django/core/management/sql.py", line 6, in <module>
from django.db import models
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
settings.py and manage.py are in the same directory and I did not have this issue before adding this plugin.
the settings.py for database section is as follows:
# Activate django-dbindexer for the default database
DATABASES['native'] = DATABASES['default']
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'}
AUTOLOAD_SITECONF = 'indexes'
INSTALLED_APPS = (
# 'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'djangotoolbox',
'autoload',
'dbindexer',
# social_auth
'httplib2',
'openid',
'oauth2',
'social_auth',
#userena
'userena',
'guardian',
'easy_thumbnails',
'accounts',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
)
and this is once I runserver
BB8265:epapyri-django$ sudo django-admin.py runserver
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 8, in <module>
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
File "/Library/Python/2.7/site-packages/django/core/servers/basehttp.py", line 26, in <module>
from django.views import static
File "/Library/Python/2.7/site-packages/django/views/static.py", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "/Library/Python/2.7/site-packages/django/utils/translation/__init__.py", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "/Library/Python/2.7/site-packages/django/utils/translation/__init__.py", line 48, in __getattr__
if settings.USE_I18N:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
[edited]
I remove the non django project from INSTALL_APP, import the django.models to my accounts/MyProfile, and chmod 777 on the .datastore since it shouldn't ask for permission every time modify it.
{File "/git/ePapyri/epapyri-django/djangoappengine/db/base.py", line 160, in _value_for_db
raise DatabaseError("Only strings and positive integers "
django.db.utils.DatabaseError: Only strings and positive integers may be used as keys on GAE.}
Why are you using django-admin.py to run syncdb and runserver? You should be using manage.py.
And you should definitely not be using sudo to run either of these.
(Also note your INSTALLED_APPS seems to be nonsense: it contains a whole lot of plain Python libraries like httplib2 and oauth2 which have no business at all being there.)
Edited
Firstly, you should have edited your question to add the traceback.
Secondly, the error message is very clear: you have not imported django.db.models in your accounts/models.py file.
Thirdly, it is still nonsense to include non-Django libraries in INSTALLED_APPS. There is simply no need to do so. And your datastore should not have been created with sudo in the first place - either delete and recreate it, or use chmod to make it accessible to a normal user.
I find out the problem is solved by adding a PIL to libraries in GAE, and yaml file would solve the issue, since it was complaining about image library in python. the link to all supported libraries for django is https://developers.google.com/appengine/docs/python/tools/libraries27, so add all the needed libraries to .yaml file and to the INSTALL_APP.