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.
Related
I'm working on a Django application and using Fabric for deployment.
When I deployed the apps to staging in Google App Engine using fab testing, I got this error:
Updating service [staging] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2022-01-01T09:48:30.226Z15496.fj.0: Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/env/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/opt/python3.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 "/env/lib/python3.6/site-packages/django/contrib/postgres/apps.py", line 8, in <module>
from .signals import register_type_handlers
File "/env/lib/python3.6/site-packages/django/contrib/postgres/signals.py", line 3, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
I'm sure the psycopg2 has been installed successfully earlier. Here's the list installed dependencies after checking by pip list:
...
platformdirs 2.4.0
prompt-toolkit 3.0.24
protobuf 3.19.1
psutil 5.5.1
psycopg2 2.8.6
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycairo 1.16.2
...
Anyone can help? Thanks!
Thank you guys for your prompt support. I've managed to resolve it.
Basically during coding I've accidentally commented the psycopg2 line in the requirements.txt. Because earlier all dependencies were installed successfully and everything is running on dev mode properly so I just kept that line excluded out.
But when deploying with Fabric, it will execute the gcloud command (gcloud app deploy) and here's the thing: GCloud will re-run everything (including installing all dependencies all over again). And the psycopg2 wasn't installed (because it's commented out) during that GCloud runtime. That's why GCloud couldn't find it for further executions.
So I've put it back and everything works like a charm. What a stupid move of mine, but it's fantastic to understand how GCloud works on its way
Just add in your requirement.txt to psycopg2=2.9.3
or run the below command manually to your virtual environment
pip install psycopg2
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.
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.