python + SQL db runserver ERROR - django

Django runserver is not work
python + SQL db. If I install sudo apt-get install python-mysqldb. Make migrate is okey, tables is create but runserver ERROR
Failed to get real commands on module "mysite": python process died with code 1: Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py", line 25, in <module>
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ivan/pycharm-2016.3.2/helpers/pycharm/_jb_manage_tasks_provider.py", line 25, in <module>
django.setup()
File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/usr/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 "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 119, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))

Make sure you have PyMySQL module installed, if not use this.
pip install pymysql
In settings.py, add following code just below import os.
Your current code
import os
Your new code
import os
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass

Related

django-admin startproject my_site problem in vscode

when i am typing command in cmd django-admin startproject my_site it is giving this error please help me universe. I tried everything from installing all to uninstalling all and it is not working and hinderancing my progress to become a web developer.
when i am typing command in cmd django-admin startproject my_site it is giving this error please help me universe. I tried everything from installing all to uninstalling all and it is not working and hinderancing my progress to become a web developer.
Traceback (most recent call last):
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\utils\timezone.py", line 10, in <module>
import zoneinfo
ModuleNotFoundError: No module named 'zoneinfo'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\CHAYAN\AppData\Local\Programs\Python\Python39-32\Scripts\django-admin.exe\__main__.py", line 9, in <module>
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\__init__.py", line 279, in fetch_command
klass = load_command_class(app_name, subcommand)
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\__init__.py", line 48, in load_command_class
module = import_module("%s.management.commands.%s" % (app_name, name))
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1021, in _gcd_import
File "<frozen importlib._bootstrap>", line 998, in _find_and_load
File "<frozen importlib._bootstrap>", line 977, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 789, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\commands\startproject.py", line 2, in <module>
from django.core.management.templates import TemplateCommand
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\core\management\templates.py", line 16, in <module>
from django.template import Context, Engine
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\template\__init__.py", line 44, in <module>
from .engine import Engine
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\template\engine.py", line
7, in <module>
from .base import Template
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\template\base.py", line 59, in <module>
from django.utils.formats import localize
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\utils\formats.py", line 9, in <module>
from django.utils import dateformat, numberformat
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\utils\dateformat.py", line 26, in <module>
from django.utils.timezone import (
File "c:\users\chayan\appdata\local\programs\python\python39-32\lib\site-packages\django\utils\timezone.py", line 12, in <module>
from backports import zoneinfo
ModuleNotFoundError: No module named 'backports'
This is happening because you are not using a virtual environment. Use the following commands to install virtual env.
pip install virtualenv
then you have to create a virtual environment, you can do that by following the command here.
python -m virtualenv myEnv # ---> whatever name you want here
and then you have to activate the environment by following command:
myEnv\Scripts\activate
and to deactivate, simply use:
deactivate

Django ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'

I know others have had a similar issues with and getting this same error, but I think my situation is unique.
I am running Django 3.1.4 and on my local machine, I can run python manage.py shell with no issue.
On the server instance, running what should be the same project, and the same version of Django, I get:
Django ImportError: cannot import name 'python_2_unicode_compatible'
from 'django.utils.encoding'
When trying to run manage.py shell. To make things more cryptic, if I open the shell on my local machine and run:
from django.utils.encoding import python_2_unicode_compatible
I get the same error. So for some reason when I call manage.py shell from my local machine it doesn't try to import python_2_unicode_compatible, but when I run it from the server it does. I can't find where the discrepancy is.
Here is the full stacktrace if that is helpful:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/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 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/chase/Env/mantis/lib/python3.8/site-packages/request/models.py", line 7, in <module>
from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (/home/chase/Env/mantis/lib/python3.8/site-packages/django/utils/encoding.py)
Any ideas on where to start poking around?
You can try this.
First, install six:
pip install six
Then go to the django.utils.encoding.py file and simply import python_2_unicode_compatible from six like this:
from six import python_2_unicode_compatible

django with celery raise No module named 'kombu' when runserver

I made a website with django and I use celery for asynchronous task, when I run:
./manage.py runserver
I got the error:
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/core/management/base.py", line 341, in run_from_argv
connections.close_all()
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/db/utils.py", line 225, in close_all
for alias in self:
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/db/utils.py", line 219, in __iter__
return iter(self.databases)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/db/utils.py", line 153, in databases
self._databases = settings.DATABASES
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/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 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
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 "/home/rouizi/OC_project13/car_rental/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/home/rouizi/OC_project13/car_rental/celery.py", line 3, in <module>
from celery import Celery
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/celery/local.py", line 509, in __getattr__
module = __import__(self._object_origins[name], None, None, [name])
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/celery/app/__init__.py", line 5, in <module>
from celery import _state
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/celery/_state.py", line 17, in <module>
from celery.utils.threads import LocalStack
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/celery/utils/__init__.py", line 9, in <module>
from kombu.utils.objects import cached_property
ModuleNotFoundError: No module named 'kombu'
I tried to install kombu:
pip install kombu
Collecting kombu
Using cached https://files.pythonhosted.org/packages/34/7e/44445f7d9031e82b4a1160141de35ca810f44041bf5065b45c68ab0ec9e8/kombu-4.6.8-py2.py3-none-any.whl
Requirement already satisfied: amqp<2.6,>=2.5.2 in ./venv/lib/python3.6/site-packages (from kombu)
Requirement already satisfied: importlib-metadata>=0.18; python_version < "3.8" in ./venv/lib/python3.6/site-packages (from kombu)
Requirement already satisfied: vine<5.0.0a1,>=1.1.3 in ./venv/lib/python3.6/site-packages (from amqp<2.6,>=2.5.2->kombu)
Requirement already satisfied: zipp>=0.5 in ./venv/lib/python3.6/site-packages (from importlib-metadata>=0.18; python_version < "3.8"->kombu)
Installing collected packages: kombu
Successfully installed kombu-4.6.8
And now I get this error if a run the server:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/apps/config.py", line 118, in create
cls = getattr(mod, cls_name)
AttributeError: module 'kombu.transport' has no attribute 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/rouizi/OC_project13/venv/lib/python3.6/site-packages/django/apps/config.py", line 136, in create
import_module(entry)
File "/usr/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 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'kombu.transport.django'
I know that the django module was definitely removed from the kombu package but if I remove kombu I get the first error, how can I fix this ?
I am using django==3.0.3, celery==4.4.1, kombu==4.6.8
You use
celery 4.4.1 has requirement kombu<4.7,>=4.6.10, but you'll have kombu 4.6.8 which is incompatible.
Please use:
pip install celery==4.4.6
pip install kombu==4.6.10
and if you want to redis you should this version
pip install redis==3.2.1
Then you to terminal and type python

ImportError: cannot import name 'python_2_unicode_compatible' when running migrations for Django-Invitations

First, I might want to mention I'm a beginner with Django. I'm trying to install Django-Invitations on my app to send sign up invitations. I followed their README instructions.
pip install django-invitations
# Add to settings.py, INSTALLED_APPS
'invitations',
# Append to urls.py
url(r'^invitations/', include('invitations.urls', namespace='invitations')),
# Run migrations
python manage.py migrate
I also pip installed all the requirements from their requirements file:
coverage==4.5.4
flake8==3.7.9
freezegun==0.3.12
mock==3.0.5
pytest==5.2.2
pytest-django==3.6.0
pytest-cov==2.8.1
tox==3.14.0
But I keep getting the same error when I run migrations for the first time:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\maxim\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\maxim\Desktop\Web Development\Projects\admin\RentQ3\myEnv\lib\site-packages\invitations\models.py", line 12, in <module>
from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible'
Any idea what the issue might be?
Thanks!
It should work if you install six.
All you then need to do is replace django.utils.encoding with six in your models.py.

Issue upgrading to Wagtail 2.1

Upgraded from Wagtail 2.0 to 2.1. Running on an Ubuntu vagrant box.
I get the error below when running any manage.py command after upgrading from Wagtail 2.0 to 2.1 or 2.11. If I downgrade back to 2.0 it works again, so I'm sure I have some incorrect dependencies in there...
Any thoughts on how to resolve this?
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/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 "/Users/chrisrogers/blog/lib/python3.6/site-packages/wagtail/search/apps.py", line 4, in <module>
from wagtail.search.signal_handlers import register_signal_handlers
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/wagtail/search/signal_handlers.py", line 3, in <module>
from wagtail.search import index
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/wagtail/search/index.py", line 10, in <module>
from modelcluster.fields import ParentalManyToManyField
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/modelcluster/fields.py", line 19, in <module>
from modelcluster.models import get_related_model, ClusterableModel
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/modelcluster/models.py", line 152, in <module>
class ClusterableModel(models.Model):
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "/Users/chrisrogers/blog/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
My req.txt is below:
Django==2.0
Jinja2==2.8
Markdown==2.6.2
MarkupSafe==0.23
Pillow>2.8.2
PyYAML==3.11
Pygments==2.0.2
Unidecode==0.04.18
Willow>0.2.2
ansible==2.0.1.0
beautifulsoup4==4.4.0
django-appconf==1.0.1
django-compressor==1.5
django-medusa==0.3.0
django-modelcluster==1.1
django-sendfile==0.3.10
django-taggit==0.18.0
django-treebeard==3.0
djangorestframework==3.7
ecdsa==0.13
elasticsearch==1.7.0
google-api-python-client==1.5.0
html5lib==0.999
httplib2==0.9.2
oauth2client==2
paramiko==1.16.0
postgres==2.1.2
psycopg2-binary==2.7.4
wagalytics==0.7
wagtail==2.1
wagtailfontawesome==1.1.3
wheel==0.24.0
You should upgrade django-modelcluster.
pip install django-modelcluster==4.1