Django Apache mod_wsgi - Unable to open database file - python

After upgrading mod_wsgi to the latest version, my app can no longer access its caching db (sqlite).
The Apache error log shows the following:
mod_wsgi (pid=1050): Target WSGI script '/location/to_my_project/project_name/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=1050): Exception occurred processing WSGI script '/location/to_my_project/project_name/wsgi.py'.
aceback (most recent call last):
File "/location/to_my_project/project_name/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/location/to/my/env/lib/python3.5/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
return WSGIHandler()
File "/location/to/my/env/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 153, in __init__
self.load_middleware()
File "/location/to/my/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 80, in load_middleware
middleware = import_string(middleware_path)
File "/location/to/my/env/lib/python3.5/site-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/location/to/my/env/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/location/to_my_project/project_name/middleware/some_middleware.py", line 16, in <module>
include_get_headers=True,
File "/location/to/my/env/lib/python3.5/site-packages/requests_cache/core.py", line 182, in install_cache
backend = backends.create_backend(backend, cache_name, backend_options)
File "/location/to/my/env/lib/python3.5/site-packages/requests_cache/backends/__init__.py", line 50, in create_backend
return registry[backend_name](cache_name, **options)
File "/location/to/my/env/lib/python3.5/site-packages/requests_cache/backends/sqlite.py", line 28, in __init__
self.responses = DbPickleDict(location + extension, 'responses', fast_save=fast_save)
File "/location/to/my/env/lib/python3.5/site-packages/requests_cache/backends/storage/dbdict.py", line 58, in __init__
with self.connection() as con:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/location/to/my/env/lib/python3.5/site-packages/requests_cache/backends/storage/dbdict.py", line 70, in connection
con = sqlite.connect(self.filename)
sqlite3.OperationalError: unable to open database file
The user running the process, has however created the file after the error has occurred. When logging in with that user, I can also open / edit the file. Other users with the same problem have reported that changing the path to the file from absolute to relative have solved their issue, but mine has been relative all along. the cache is also created in the correct directory.
mod_wsgi version: 4.5.14 (previous version 4.3.0)
Django: 1.10
Apache: 2.4.18
Server: Ubuntu 16.04
I have not changed the python path after upgrading the wsgi module. Any suggestions on what might be wrong?

Related

Django runserver error: _frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('django.test.signals') at 139824587568800

After adding 'rest_framework.authtoken' to INSTALLED_APPS (settings.py) and running python manage.py migrate I started to get a strange error when trying to run django server (the strange thing is, that the error appears on every 2nd or 3rd attempt, sometimes the server starts running without the error):
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__
return self._engines[alias]
KeyError: 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/username/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/home/username/anaconda3/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/contrib/admin/checks.py", line 79, in check_dependencies
for engine in engines.all():
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
return [self[alias] for alias in self]
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
return [self[alias] for alias in self]
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
engine = engine_cls(params)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
options['libraries'] = self.get_templatetag_libraries(libraries)
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
libraries = get_installed_libraries()
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
for name in get_package_libraries(pkg):
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
module = import_module(entry[1])
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/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 "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/rest_framework/templatetags/rest_framework.py", line 12, in <module>
from rest_framework.renderers import HTMLFormRenderer
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/rest_framework/renderers.py", line 19, in <module>
from django.test.client import encode_multipart
File "/home/username/.local/share/virtualenvs/blogapi-nhgDNK6P/lib/python3.7/site-packages/django/test/client.py", line 23, in <module>
from django.test import signals
File "<frozen importlib._bootstrap>", line 980, in _find_and_load
File "<frozen importlib._bootstrap>", line 149, in __enter__
File "<frozen importlib._bootstrap>", line 94, in acquire
_frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('django.test.signals') at 139824587568800
I am using: django 2.2.7 and django rest_framework 3.10.3. Thanks for any help.
There's a ticket on Django's issue tracker discussing similar issue: https://code.djangoproject.com/ticket/30352.
For a temporary fix, you can remove all .pyc files on your project and run the runserver command again.
find <path-to-your-project> -type f -name '*.pyc' -delete
NB: I use pyenv which stores the project's virtual environment in a different directory outside the project directory. Removing only the project's .pyc files work for me, but you may also try removing the virtual environment's .pyc files if needed.
For anyone who still gets this error, please kindly comment in the issue page to help Django developers reproduce and fix the error.

ModuleNotFoundError: No module named 'admin'

I have this new remote job, where I had to clone all the code from a repository, and I have to make an export of the database from MySQL hosted in RDS.
The first problem is that when I set up the configuration to start the app, it raise an error telling me this:
Run Configuration Error: Broken configuration due to unavailable plugin or invalid configuration data.
The other thing is that I already have the data dumped and set up in my local storage (the app works this way, is no longer using AWS Cloud) but when I try to do an python manage.py migrate , this error comes up...
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\django\core\management\__init__.py", line 312, in execute
django.setup()
File "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\django\apps\config.py", line 86, in create
module = import_module(entry)
File "C:\Users\Tony-App\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\Tony-App\Documents\App\venv\lib\site-packages\liststyle\__init__.py", line 1, in <module>
from admin import ListStyleAdminMixin
ModuleNotFoundError: No module named 'admin'
First, django-liststyle is a dependency in your project, so make sure it has been installed.
py -m pip install django-liststyle
Second, make sure to add that package to INSTALLED_APPS:
INSTALLED_APPS = {
...
'liststyle',
...
'django.contrib.admin',
...
}
Third, I think the error you are getting is because of the way you are importing ListStyleAdminMixin. This should be the correct way to import it:
from liststyle.admin import ListStyleAdminMixin

Django can't migrate on Heroku

I have problem with migrating my database on Heroku trying to run heroku run python manage.py migrate. I always get this error:
(venv) E:\Studia\advise\promotion>heroku run python manage.py migrate
Running python manage.py migrate on ⬢ pro-motion... up, run.6349 (Free)
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 325, in execute
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 157, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/promotion/settings.py", line 27, in <module>
DEBUG = config('DEBUG', default=False, cast=bool)
File "/app/.heroku/python/lib/python3.6/site-packages/decouple.py", line 197, in __call__
return self.config(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/decouple.py", line 85, in __call__
return self.get(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/decouple.py", line 79, in get
return cast(value)
File "/app/.heroku/python/lib/python3.6/site-packages/decouple.py", line 50, in _cast_boolean
raise ValueError('Not a boolean: %s' % value)
ValueError: Not a boolean: True
Your screenshot shows a DEBUG environment variable whose value contains extra whitespace.
Depending on how you use that variable, the whitespace could easily caus problems. Delete that extra whitespace and see if that fixes things.
Also, you've partly revealed your SECRET_KEY and PostgreSQL credentials. Please change the SECRET_KEY and rotate your database credentials. Your current values are no longer secure.

Celery + Django error: TypeError: 'Settings' object is not subscriptable

I'm running into this issue and I have absolutely no idea where it comes from.
I'm using exactly the same code provided in the Celery tutorial except for the fact that I'm using the project directory also as my unique app directory (so, having models.py, views.py and adding 'proj' to INSTALLED_APPS).
The weird part is, while trying this setup on my local machine, the error always appeared with every django command (migrate, shell...). Removing the virtualenv and reinstalling misteriously fixed it. Now, when pushing the app to Heroku I'm having the same issue and can't make it work.
Any hints?
Thanks!
ps: Python 3.4.2, Django 1.8.4, Celery 3.1.18
The error traceback:
Running `celery -A proj worker -l info` attached to terminal... up, run.7187
Traceback (most recent call last):
File "/app/.heroku/python/bin/celery", line 9, in <module>
load_entry_point('celery==3.1.0', 'console_scripts', 'celery')()
File "/app/.heroku/python/lib/python3.4/site-packages/celery/__main__.py", line 29, in main
main()
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/celery.py", line 80, in main
cmd.execute_from_commandline(argv)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/celery.py", line 732, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/base.py", line 299, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/base.py", line 429, in setup_app_from_commandline
self.app = self.find_app(app)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/base.py", line 449, in find_app
sym = self.symbol_by_name(app)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/bin/base.py", line 474, in symbol_by_name
return symbol_by_name(name, imp=import_from_cwd)
File "/app/.heroku/python/lib/python3.4/site-packages/kombu/utils/__init__.py", line 92, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/app/.heroku/python/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/app/proj/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/app/proj/celery.py", line 17, in <module>
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.4/site-packages/celery/app/base.py", line 277, in autodiscover_tasks
if self.conf.CELERY_FORCE_BILLIARD_LOGGING:
File "/app/.heroku/python/lib/python3.4/site-packages/celery/datastructures.py", line 336, in __getattr__
return self[k]
File "/app/.heroku/python/lib/python3.4/site-packages/celery/datastructures.py", line 458, in __getitem__
return d[key]
File "/app/.heroku/python/lib/python3.4/site-packages/django/utils/functional.py", line 227, in inner
return func(self._wrapped, *args)
TypeError: 'Settings' object is not subscriptable
You're following Celery 4.0's document, but you're using Celery 3.1.18,
This gives you an error.
Upgrade your installed celery over than 4.0 with pip.
pip install -U celery

Cannot install MySQL 5.5 with django 1.8.2 on Windows 7 64bit

I had MySQL 5.5 installed as part of XAMPP stack on Win7 64bit. I am using python 3.4 and I want to try out Django. So I installed Django using pip from command line and it worked fine with the default sqlite3 db.
To integrate MySQL, I see installed 'mysql-connector-python' using pip. I changed the engine to ''mysql.connector.django'. I changed the settings.py file with the DATABASE settings as follows:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'db',
'USER': 'root',
'PASSWORD': '',
}
}
When I check the server, I get the following error:
Traceback (most recent call last):
File "c:\Python34\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "c:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "c:\Python34\lib\site-packages\mysql\connector\django\base.py", line 50, in <module>
from django.db.backends import (BaseDatabaseFeatures, BaseDatabaseOperations,
ImportError: cannot import name 'BaseDatabaseFeatures'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "c:\Python34\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "c:\Python34\lib\site-packages\django\core\management\__init__.py", line 312, in execute
django.setup()
File "c:\Python34\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "c:\Python34\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "c:\Python34\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "c:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "c:\Python34\lib\site-packages\django\contrib\auth\models.py", line 41, in <module>
class Permission(models.Model):
File "c:\Python34\lib\site-packages\django\db\models\base.py", line 139, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "c:\Python34\lib\site-packages\django\db\models\base.py", line 324, in add_to_class
value.contribute_to_class(cls, name)
File "c:\Python34\lib\site-packages\django\db\models\options.py", line 250, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "c:\Python34\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "c:\Python34\lib\site-packages\django\db\utils.py", line 240, in __getitem__
backend = load_backend(db['ENGINE'])
File "c:\Python34\lib\site-packages\django\db\utils.py", line 129, in load_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'base', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name 'BaseDatabaseFeatures'
Found a bug filed here-
https://bugs.mysql.com/bug.php?id=76752
For resolution, it suggests-
Adjust the backend to the changes in Django API. See the release notes for details:
https://docs.djangoproject.com/en/1.8/releases/1.8/#database-backend-api
How do I adjust the backend? I have no idea how to proceed.
Installing from the forked version works.
pip install git+https://github.com/multiplay/mysql-connector-python
From Github repo description this one is
Fork of version 2.0.4 of mysql-connector-python (and probably rebased
at points) until upstream issues are fixed
Got the reference from bug url.
The latest version of mysql-connector-python works with Django 1.8. You can get it from the mysql website.
(version mysql-connector-python-2.1.3)
Install it from the MySQL:
pip install http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz

Categories