I migrated my website from sqlite3 to postgresql. I'm deploying my website in heroku but this error came up when I'm executing command
push heroku master
-----> Python app detected
! Python has released a security update! Please consider upgrading to python-3.7.3
Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Installing python-3.7.2
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
Installing dependencies from Pipfile…
-----> Installing SQLite3
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
ModuleNotFoundError: 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 "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/app/.heroku/python/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/build_bda0f9a0e468e1cd92eba305080085ba/home/models.py", line 3, in <module>
from wagtail.core.models import Page
File "/app/.heroku/python/lib/python3.7/site-packages/wagtail/core/models.py", line 8, in <module>
from django.contrib.auth.models import Group, Permission
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
I don't know why it says import error since I already installed psycopg2 in my virtual environment. The python shell does not even throw some errors when I import it and I can run my server successfully in my local machine
In my requirements.txt file, psycopg2==2.8.3 is listed there.
My Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "<2.3,>=2.2"
wagtail = "<2.6,>=2.5"
[requires]
python_version = "3.7"
My database configuration in base.py of settings folder
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'personalsite',
}
}
in production.py of my settings folder
DATABASES['default'] = dj_database_url.config()
in requirements.txt
psycopg2==2.8.2
beautifulsoup4==4.6.0
certifi==2019.6.16
chardet==3.0.4
dj-database-url==0.5.0
dj-static==0.0.6
Django==2.2.2
django-modelcluster==4.4
django-taggit==0.24.0
django-toolbelt==0.0.1
django-treebeard==4.3
djangorestframework==3.9.4
draftjs-exporter==2.1.6
gunicorn==19.9.0
html5lib==1.0.1
idna==2.8
Pillow==5.4.1
pytz==2019.1
requests==2.22.0
six==1.12.0
sqlparse==0.3.0
static3==0.7.0
Unidecode==1.1.0
urllib3==1.25.3
wagtail==2.5.1
webencodings==0.5.1
Willow==1.1
It seems like you installed all those packages to your local Python installation and not to your virtual environment. Your Pipfile doesn't reflect the packages such as psycopg2, dj_database_url etc. Please install the packages to your virtual environment.
First, activate your virtual environment,
pipenv shell
Then install the following packages,
pipenv install psycopg2-binary
pipenv install dj-database-url
Run pipenv lock to generate the appropriate Pipfile.lock. And try to push it to Heroku once again using the following command.
push heroku master
Related
I was trying to run a Django project which I got from someone else.
But I can't do the migration.
I came from a mobile app development background. So I'm quite new to this backend thing including Django.
Please have a look and help with this issue. Thank you
Traceback (most recent call last):
File "/Users/punreachrany/Desktop/MyProject/manage.py", line 22, in <module>
main()
File "/Users/punreachrany/Desktop/MyProject/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/apps/config.py", line 228, in create
import_module(entry)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'bootstrap4'
Bootstrap is a dependency you need to add. You can install it by running the below command before migrating.(This is a one time setup)
python -m pip install bootstrap4
Since you mentioned you got the project from someone else.In that case check if there is a file called requirements.txt.
If it exists, run the below command from that directory to install all the required dependencies.
Python -m pip install -r requirements.txt
The pip module Im using needs to be included in installed apps as such:
INSTALLED_APPS = [
...,
'django_apscheduler',
...
]
But I keep getting the following when I build my app:
ModuleNotFoundError: No module named 'django_apscheduler'
Even though the app is clearly listed in my pip list. The module in question is Django APScheduler.
EDIT: Ive already installed the module via pip install django_apscheduler, but it is listed in pip list as:
django-apscheduler # using - as opposed to _
EDIT 2: Full trace:
+ python3 manage.py test --settings=grin_app.settings.test
Traceback (most recent call last):
File "manage.py", line 25, in <module>
execute_from_command_line(sys.argv)
File "/root/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/root/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/root/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/root/.local/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/root/.local/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/local/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 'django_apscheduler'
this error happens when you have NOT installed the app.
run pip install django_apscheduler and try again.
make sure you are installing it in your python environment or in your appropriate virtual environment
I tried several times now to deploy my Heroku application but I always receive the attached error message. After adding DISABLE_COLLECTSTATIC=1 it works and I can remove it after. But it never works when it's enabled which it should be. Here a picture of my folder structure if it's needed https://drive.google.com/file/d/11L8jpFzYfDYT3Ob4G-soaGhL0pgU24iZ/view?usp=sharing
! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
Detected buildpacks: Python,Node.js
See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Python app detected
-----> Installing python-3.6.6
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
Installing dependencies from Pipfile.lock (d438cb)…
-----> Installing SQLite3
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
value.contribute_to_class(cls, name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 199, in __getitem__
self.ensure_defaults(alias)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 171, in ensure_defaults
conn.setdefault('ATOMIC_REQUESTS', False)
AttributeError: 'NoneType' object has no attribute 'setdefault'
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
It might be unrelated to collectstatic. Similar exception was described here: python error AttributeError: 'str' object has no attribute 'setdefault'.
I'd suggest checking your DATABASES setting in your settings.py. Check out django-heroku (https://github.com/heroku/django-heroku) if you're not using it already and just use this in your settings:
import django_heroku
django_heroku.settings(locals())
I am setting up the new Django project with PostgreSQL as a backend . I am using the pip to install the packages . Following are the requirements are install in new environment.
Django==1.9
argparse==1.2.1
djangorestframework==3.3.3
psycopg2==2.6.1
wsgiref==0.1.2
I don't know where I made mistakes. please Help me out to configure the new project.please give me step by step procedure to configure the new environment.
The error stack is
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
ImportError: cannot import name DateRange
my#box:~ mkvirtualenv env
(env)my#box:~ pip install -r requirements.txt
(env)my#box:~ django-admin startproject proj
(env)my#box:~ ./proj/manage.py shell
>>> from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
>>>
Works for me. Can you import psycopg2 and import psycopg2.extras?
I know this is way too late to the party, but apparently using psycopg2-binary worked for me.
This was with Djano 2.2.9 and Djongo 1.3.0 that failed to import psycopg2 with the following error:
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 "/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/venv/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/venv/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
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 "/segnet-webapp/webapp/label/models.py", line 2, in <module>
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
ModuleNotFoundError: No module named 'psycopg2'
I'm posting this here since it is one of the very few places that show up when searching for this particular error.
All I had to do was:
$(venv) pip install psycopg2-binary
I am deploying my app to heroku. I stuck at this code:
$ heroku run python mysite/manage.py migrate
The output are:
Running `python mysite/manage.py migrate` attached to terminal... up, run.8367
Traceback (most recent call last):
File "/app/mysite/django/apps/config.py", line 118, in create
cls = getattr(mod, cls_name)
AttributeError: 'module' object has no attribute 'staticfiles'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "mysite/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/mysite/django/core/management/__init__.py", line 385, in execute_fr
om_command_line
utility.execute()
File "/app/mysite/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/app/mysite/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/mysite/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/app/mysite/django/apps/config.py", line 123, in create
import_module(entry)
File "/app/.heroku/python/lib/python3.3/importlib/__init__.py", line 90, in im
port_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1529, in _find_and_load_unlocked
ImportError: No module named 'django.contrib.staticfiles'
Could anyone helps me with that? I searched a lot on staticfiles, but it does not work...
Double-check that your staticfiles contrib is installed via:
pip install django-staticfiles
Note: If pip points to your Python 2 instance, then use pip3 instead.
Then verify your PYTHONPATH if it has the correct paths.