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.
Related
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.
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
Every time I am running the application from the Github Repository, I am getting the following error:
Traceback (most recent call last):
File "C:/Users/lenovo-pc/Desktop/Django-Social-Authentication-master/socialauth/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 338, in execute
django.setup()
File "C:\Python35\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python35\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Python35\lib\site-packages\django\apps\config.py", line 94, in create
module = import_module(entry)
File "C:\Python35\lib\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 956, in _find_and_load_unlocked
ImportError: No module named 'social_django'
I want to know what is social_django all about? I have installed many libraries, but non of then have satisfied this error:
python-social-auth[django]
social-auth-app-django
But nothing happens. Please let me know what I should do.
The Python executable you use to run the application doesn't have social_django installed. You can verify this by running these commands:
python manage.py shell
import social_django
This will give you an error, for the same reason.
You can install it with:
pip install social-auth-app-django
The github project you are referring to, has dependency on package 'social_django'. You can check it here.
So you would need to install 'social_django' that can be installed by
pip install social-auth-app-django
I know, you have mentioned this in the question itself but I am pretty sure that the package is not installed.
Strange, this happened after i upgraded to 1.10.3 on Ubuntu 16, Anaconda Distribution. The old 0.9 worked fine for me. Reinstalled multiple times as well, no difference.
➜ django-admin startproject django_tutorial
Traceback (most recent call last):
File "/storage/programfiles/anaconda3/bin/django-admin", line 11, in <module>
sys.exit(execute_from_command_line())
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/django/core/management/__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/storage/programfiles/anaconda3/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 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'dash'
Can't find anything on google, anyone have any ideas? :)
EDIT:
Django-admin is installed at:
➜ ~ which django-admin
/storage/programfiles/anaconda3/bin/django-admin
Stuff I've tried so far:
1) Multiple pip uninstall Django and pip install Django
2) Verifying site-packages has Django uninstalled and reinstalling again
3) After uninstall, pip freeze | grep Django shows nothing.
4) pip install django-dash
Solves it, but now the error is ImportError: No module named 'dash.development_settings'
Strange:
python -m django --version
throws the same ImportError: No module named 'dash.development_settings'
but I can run python in a terminal and do
import django
django.get_version()
and it works fine
You don't have django-dash installed
pip install django-dash
Or remove dash and dash. related apps from INSTALLED_APPS
I finally figured out what the hell was wrong.
my bashrc had export DJANGO_SETTINGS = dash.development_settings or something like that (deleted it already) and removing that line worked.
Question Solved, it's just a typo in file mysite/polls/models.py. Thanks everyone for the help!
Environment: Ubuntu 14.04 with Python 2.7 and 3.4 preinstalled (default is 2.7) and Django: 1.8.4.
I'm a newbie to Django and trying to follow the Django 1.8 tutorial.
After finished part1, since tutorial was specially designed for python3, not python2, I tried to change Python version, with the following command:
alias python=python3
Now the problem arise: when I run python manage.py createsuperuser, I get the following error:
ImportError: No module named django
I searched on the web for solutions and ended up with this command
python -c "import django; print(django.get_version())"
After executing this line, the error changes to:
ImportError: No module named djangeo.db
I'm stucked here:( I coundn't find a proper solution for this onlne.
There a similar problem on AskUbuntu but it also says hardcoding system path is not a very good idea.
Can anyone help me with this, please?
After installing the Django with pip3, the error message looks like follows:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, 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 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/julia/mysite/polls/models.py", line 5, in <module>
from djangeo.db import models
ImportError: No module named 'djangeo'
You have not installed Django under python3.
Modules only work for the version of python they were installed for. pip installs modules for python2 by default. To install modules for python3, use pip3
First verify you have pip3 installed:
sudo apt-get install python3-pip
then:
sudo pip3 install django
for future projects, you should consider setting up a virtualenv.