Dokku Compilation Error - django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

喜欢而已 提交于 2021-02-11 14:54:17

问题


I've been attempting to setup my built-up Django Instance as a Database Server. Had chosen DigitalOcean as my platform and had read that Dokku is a useful PaaS system that will enable better scalability for this API I'm trying to deploy.

I have been at this problem for the last 3-4 days straight and really had gone through every potential means of solution I could have found online.

Being more of a front-end developer, I'm pretty bad at this backend installation matter.

At first I thought that Dokku was sort of a Git-push function where I will push from localhost -> Dokku -> Git -> Deploy.

Digging deep, I setup Public-Private keys for Dokku to authorize a Git push to Github.

Ultimately, after finally giving up on that route, I did realised I was in the wrong and roughly the way Dokku should work is localhost -> Git push -> Dokku -> Deploy.

Been at this for the last 15-18 hrs of development. The flow seemed to work, where I didn't really need to do much installations on the DigitalOcean droplet.

However, the biggest issue I am facing is with this


My current install is as so:

  • DigitalOcean Droplet 1-click Dokku VERSION 0.14.6
  • OS Ubuntu 18.04

I have added all suggested files as I have found in online blogs and forums of the following under the root folder:


requirements.txt
---
absl-py==0.4.0
astor==0.7.1
backports.weakref==1.0.post1
dj-database-url==0.5.0
Django==1.11.20
django-cors-headers==3.0.0
django-filter==2.0.0
djangorestframework==3.9.4
enum34==1.1.6
funcsigs==1.0.2
futures==3.2.0
gast==0.2.0
GDAL==2.4.1
grpcio==1.14.1
h5py==2.8.0
Keras==2.2.2
Keras-Applications==1.0.4
Keras-Preprocessing==1.0.2
Markdown==2.6.11
mock==2.0.0
nose==1.3.7
numpy==1.16.3
pbr==4.2.0
Pillow==6.0.0
protobuf==3.6.1
psycopg2==2.8.2
pytz==2019.1
PyYAML==3.13
scipy==1.1.0
six==1.11.0
sqlparse==0.3.0
tensorboard==1.10.0
tensorflow==1.10.1
termcolor==1.1.0
Theano==1.0.2
virtualenv==16.5.0
Werkzeug==0.14.1
whitenoise==4.1.2


---
runtime.txt
---
<code>python-3.6.8</code>



app.json
---
{
    "scripts": {
      "dokku": {
        "postdeploy": "./manage.py migrate"
      }
    }
  }



---
Procfile
---
web: gunicorn core.wsgi — log-file -



---
settings.py
---
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    },
    'original': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }

}

I was expecting after typing "git push dokku master" the compilation will give me a deployment url which I can view and launch the Django server.

However, what I was face was:



Enumerating objects: 8664, done.
Counting objects: 100% (8664/8664), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5363/5363), done.
Writing objects: 100% (8664/8664), 17.23 MiB | 3.67 MiB/s, done.
Total 8664 (delta 2186), reused 8568 (delta 2157)
remote: Resolving deltas: 100% (2186/2186), done.
-----> Cleaning up...
-----> Building apihirebred from herokuish...
-----> Adding BUILD_ENV to build environment...
-----> Python app detected
-----> Installing python-3.6.8
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
       Installing dependencies from Pipfile…
-----> Noticed GDAL. Bootstrapping gdal.
-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
       File "/app/.heroku/python/lib/python3.6/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 22, in <module>
       execute_from_command_line(sys.argv)
       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
       utility.execute()
       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, 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 114, in populate
       app_config.import_models()
       File "/app/.heroku/python/lib/python3.6/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.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 117, 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 321, 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 204, 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 28, in __getattr__
       return getattr(connections[DEFAULT_DB_ALIAS], item)
       File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 201, in __getitem__
       backend = load_backend(db['ENGINE'])
       File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
       return import_module('%s.base' % backend_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 "/app/.heroku/python/lib/python3.6/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



More specifically:

"django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'" error, that kept on persisting no matter how I have tried to resolve the bug.
: :
! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'dokku@server'


No matter how I have tried to install on both Dokku and my localhost for "psycopg2", I was unable to resolve the bug.

I sincerely hope someone can assist me with the above, as I am no expert on this matter with Dokku or how it really compiles the package and why the compiler is unable to retrieve "psycopg2" for the deployment.

I also hope this will not be the only issue moving forward as well.

Hope to hear your expert advice. Thanks!


回答1:


try this docker command to install psycopg2 adapter

docker-compose exec web pipenv install psycopg2-binary==2.8.3

you can also mention your own specific version if you like.

if you still get an error it means the container is not stopped properly. Use below command to stop it

docker-compose down

and then start it after installing psycopg2

docker-compose up 


来源:https://stackoverflow.com/questions/56100852/dokku-compilation-error-django-core-exceptions-improperlyconfigured-error-loa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!