Django Runserver error while executing "Python manage.py runserver" - python

C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\Scripts\FirstDjango
Proj>python manage.py runserver
Exception ignored in thread started by: <function check_errors.<locals>.wrapper
at 0x00000000038D00D0>
Traceback (most recent call last):
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\core\management\commands\runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\utils\autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\apps\config.py", line 94, in create
module = import_module(entry)
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\importl
ib\__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\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\contrib\admin\__init__.py", line 4, in <module>
from django.contrib.admin.filters import (
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\contrib\admin\filters.py", line 10, in <module>
from django.contrib.admin.options import IncorrectLookupParameters
File "C:\Users\SRIRAM_CHIVO\AppData\Local\Programs\Python\Python38\lib\site-pa
ckages\django\contrib\admin\options.py", line 12, in <module>
from django.contrib.admin import helpers, widgets
SyntaxError: Generator expression must be parenthesized (widgets.py, line 152)
Django version installed successfully-1.11.10
python version-3.8.1
Project created and manage.py as well created.
BUt while i am trying runserver,Throwing an error with this this big passage:
Help me out how can i solve this?

The same happened with me also, but this issue is a minor one as it happens if you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want. So you should start from the beginning, uninstall Django first, then create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
Now, In the command prompt, ensure your virtual environment is active, and execute the following command:
py -m pip install Django
NOTE: If django-admin only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts in PATH. This usually occurs when there is more than one Python version installed.

Related

Django DLL load failed: The specified module could not be found

Hello i m new at django. I installed all moduoles from anaconda. Then created a web application with
django-admin startproject
My project crated successfully. No problem
Then i tried to run that project at localhost to see is everything okay or not. And i run that code in command line
python manage.py runserver
And i get that error:
Unhandled exception in thread started by <function check_errors.
<locals>.wrapper at 0x00000221B6D45A60>
Traceback (most recent call last):
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\core\management\commands\runserver.py", line 109, in
inner_run
autoreload.raise_last_exception()
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\core\management\__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\Sercan\Anaconda3\lib\site-packages\django\__init__.py",
line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\Sercan\Anaconda3\lib\site-packages\django\apps\config.py",
line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Sercan\Anaconda3\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\Sercan\Anaconda3\lib\site-
packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser,
BaseUserManager
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\db\models\base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\db\models\base.py", line 305, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\db\models\options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table,
connection.ops.max_name_length())
File "C:\Users\Sercan\Anaconda3\lib\site-packages\django\db\__init__.py",
line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\Sercan\Anaconda3\lib\site-packages\django\db\utils.py",
line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\Sercan\Anaconda3\lib\site-packages\django\db\utils.py",
line 110, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\Sercan\Anaconda3\lib\importlib\__init__.py", line 127, in
import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Sercan\Anaconda3\lib\site-
packages\django\db\backends\sqlite3\base.py", line 10, in <module>
from sqlite3 import dbapi2 as Database
File "C:\Users\Sercan\Anaconda3\lib\sqlite3\__init__.py", line 23, in
<module>
from sqlite3.dbapi2 import *
File "C:\Users\Sercan\Anaconda3\lib\sqlite3\dbapi2.py", line 27, in
<module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
Can someone tell me where do i make mistake and how can i fix this problem ?
I had this problem. I solved it by running it in the Anaconda shell.
Open Anaconda Shell/terminal by pressing your Windows key and searching Anaconda
Go to the directory you have your django project in
python manage.py runserver
It sounds like you need to install SQLite:
https://www.sqlite.org/download.html
Or you could change the database settings in your settings file to use some other database.
if you want to use anaconda then follow below steps
conda create --name MyDjangoEnv(virtual environment) Django
press y to install. before press y please make sure correct version of software are selected
activate myDjangoEnv
conda info --envs
conda install django
conda install sqlparse
django-admin startproject first_project
Remove anaconda
Download and install from python.org in c:\python37. Here it will be easy to set variables
Setup python variables
Don't forget to select pip while installing python.
Path:c:\python37,c:\python32\Scripts
If you want to install django on a virtual environment install virtualevmwrapper-win
Voila! It works for me.
I found a solution in this site: http://felipegalvao.com.br/blog/2017/01/03/como-criar-ambientes-e-instalar-o-django-com-distribuicao-anaconda/
Basically, you need to activate an environment in your anaconda prompt.
Step 1: conda info --envs
Step 2 : conda create --name env_name python=3
Step 3: pip install django
Step 4: Assuming that you already created a startproject, run the manage.py runserver
Thanks!
I was facing the same problem, it simply means that dll module is not installed in that path while creating a project don't go with first option i.e venv(virtual environment) this will not let modules to import in your project.., go with the second option for interpreter and select your respective python.exe.
run the below commands in terminal
django-admin
if it shows error then
pip install django
thanks.

Django No module named 'django.db.migrations.migration'

I had a custom user model in my project. I wanted to delete it and return default user model by deleting all tables in my database and deleting migrations. After this, I tried to run python manage.py makemigrations command but it writes:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
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 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/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/contrib/contenttypes/apps.py", line 9, in <module>
from .management import (
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/contrib/contenttypes/management/__init__.py", line 2, in <module>
from django.db import DEFAULT_DB_ALIAS, migrations, router, transaction
File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/db/migrations/__init__.py", line 1, in <module>
from .migration import Migration, swappable_dependency # NOQA
ModuleNotFoundError: No module named 'django.db.migrations.migration'
I have no idea what I did wrong.
Your Django installation seems corrupted. You can reinstall with:
pip3 uninstall Django
pip3 install Django
Usually this happens if you installed Django in your regular operating system's python installation and then later installed a virtualenv copy and tried to run the django server. You need to reinstall Django in your virtualenv, after removing it first if it already exists.
Assuming you are running in the virtual environment, you might have actually deleted the files under /django/db/migrations/ as well. So the error is obvious here. You would need to reinstall django to get the files back for successful migration.
To do this, check the django version
python -m django --version
Then install the same version using following command
pip install --upgrade --force-reinstall Django==3.2.2

JetBrains Pycharm - Getting error while debugging my project

I just switched to Mac from windows.
I imported my project in pycharm from GitHub but it is giving unknown errors.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10b75f6a8>
Traceback (most recent call last):
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/Users/rajababu/anaconda/lib/python3.6/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 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'import_export'
I think import_export is not in your INSTALLED_APPS.
Install import_export with pip
pip install django-import-export
and try again.
You need to install the import_export plugin. You can do this in PyCharm using File > Settings > Project Interpreter and press the "+" icon (search for it and install it). Make sure to add import_export to your INSTALLED_APPS variable in your settings as well.

Django not connecting to MySQL properly, throws "ImportError: cannot import name 'NULL'"

I want to use Django (using Python 3) with MySQL. To do so, I installed mysqlclient. To first test the waters with MySQL, I also created a new Django project and app, changed the DATABASES configuration in "settings.py" to settings I prepared in MySQL, and ensured that MySQL server is running (the database I created for testing does not have any tables within it at this point).
But when I do
python manage.py runserver
in terminal command, I get the following error:
ImportError: No module named '_mysql_exceptions'
ImportError: No module named '_mysql_exceptions'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103efaea0>
Traceback (most recent call last):
File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/shtryts/Django-1.10.1/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/shtryts/Django-1.10.1/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/shtryts/Django-1.10.1/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/shtryts/Django-1.10.1/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/shtryts/Django-1.10.1/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/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 "/Users/shtryts/Django-1.10.1/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/shtryts/Django-1.10.1/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/shtryts/Django-1.10.1/django/db/models/base.py", line 119, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/shtryts/Django-1.10.1/django/db/models/base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/shtryts/Django-1.10.1/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/shtryts/Django-1.10.1/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/shtryts/Django-1.10.1/django/db/utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/shtryts/Django-1.10.1/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/shtryts/Django-1.10.1/django/db/backends/mysql/base.py", line 31, in <module>
from MySQLdb.converters import Thing2Literal, conversions # isort:skip
File "/Users/shtryts/.virtualenvs/django/lib/python3.5/site-packages/MySQLdb/converters.py", line 35, in <module>
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
ImportError: cannot import name 'NULL'
Should I be modifying a .py file within MySQLdb folder? If so, what should I do so that the Django project properly connects with the database?
I'm testing in a virtualenv, Mac OS X 10.10.3. Thanks in advance for the help. (Also, I did not find a similar question as mine, but apologies if this has been addressed here before).
EDIT: I failed to share my DATABASE setting, so here it is.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test1',
'USER': 'admin',
'PASSWORD': 'dbadmin',
'HOST': 'localhost',
'PORT': '3306',
}
}
Settings.py, the django project I created for testing purposes, mysqlclient installs, django framework, they're all inside virtualenv. Originally, mysqlclient (includes MySQLdb) was installed via "pip install" under the path /Library/Frameworks/Python.framework/Versions/3.5... I made a copy of the installed folders and placed them under the python site-package folder within virtualenv. From what I understand Django does not have a problem locating those folders and files.
which pip output: /Users/shtryts/.virtualenvs/django/bin/pip. And my own code is in .virtualenvs/django/test/mySQLtest
Ok, there are several problems here.
First of all you cannot copy files from one python installation or virtual installation to another python installation or virtualenv. Please delete your current virtualenv and make a new one, detailed instructions are here: http://docs.python-guide.org/en/latest/dev/virtualenvs/
but briefly
rm -rf /Users/shtryts/.virtualenvs/django
virtualenv /Users/shtryts/.virtualenvs/myvirtualenv
I recommend not using django as the name of the virtualenv. Use a name that shows which version of python is being used. Now activate the virtualenv before going to the next step.
Then you should install all the requirements with pip such as
pip install django
Please note that 'MysqlDB' is not compatible with python 3x please see here for details: https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers
Last but not least. one does not put one's own code inisde the django installation folders. instead in your home or workspace
django-admin starproject myproject

Error running Python 3.4 with PostgreSQL (psycopg2) and Django

So if I start Python 2.7's shell with python, and run import psycopg2, I don't receive any errors. However, if I try to run import psycopg2 in Python 3.4's shell, I get: ImportError: No module named 'psycopg2'. On the other hand, importing django works fine on both shells (after using pip3 to install it).
Secondly, if I run python3 manage.py runserver, I get an error (see below), whereas I don't if I use just python. I think this is because of the first problem I described but I'm not sure (sorry if this is a stupid question, I'm really new to Django - trying to get through the tutorial).
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 23, in < module >
import psycopg2 as Database
ImportError: No module named 'psycopg2'
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 "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/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 "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in < module >
class Permission(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, * * kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 297, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/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 "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in < module >
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
What can I do to fix it?
Additionally, I'm super confused on this whole Python 2 vs 3 business with Django (sorry again if this is a really basic question). When you run django-admin.py startproject theproject, how do you specify which version of Python you want to use with Django? Is it as simple as just writing your code in Python 3 and running everything with python3 as opposed to python?
Make sure you have run pip3 install psycopg2.
By default, django-admin.py will run using Python 2. That doesn't matter, since all it's doing is creating the folder layout the project.
Once the project has been created, you can write Python 3 code, and run your site using python3 manage.py runserver to use Python 3.
Later on, you might want to investigate virtual environments. If you create your virtual environment to use Python 3
virtualenv v -p python3
Then you can run
./manage.py runserver
in your virtual environment, and it will use Python 3.
I ran into the same problem as I switched from using Python 3 from Python 2.7 using virtualenv on a Mac. Apparently, while trying to pip install psycopg2 from Python 3 it was still using the wheel created during the install made using Python 2.7. So I had to rebuild the wheel using
sudo -H pip3 install --upgrade --force-reinstall psycopg2
If you have multiple python versions installed then the best solution to install the psycopg is to download the archive from
http://initd.org/psycopg/download/
and then install in by running (in my case, I wanted to install it for python 3.4)
python3.4 setup.py build
sudo python3.4 setup.py install
Now, check if it is installed by typing:
python3.4
help()
modules
It should display psycopg2 in the list of modules.

Categories

Resources