Dokku编译错误-django.core.exceptions.ImproperlyConfigured:加载psycopg2模块时出错:没有名为“ psycopg2”的模块

时间:2019-05-12 15:43:04

标签: django postgresql psycopg2 digital-ocean dokku

我一直在尝试将内置的Django实例设置为数据库服务器。选择DigitalOcean作为我的平台,并已阅读Dokku是有用的PaaS系统,它将为我尝试部署的该API提供更好的可伸缩性。

过去3-4天,我一直在解决这个问题,而且确实经历了我在网上可以找到的所有可能解决方案。

作为一名前端开发人员,我在此后端安装问题上非常不好。

起初我以为Dokku是一种Git-push函数,我将从 localhost-> Dokku-> Git->部署进行推送。

深入研究,我为Dokku设置了公私钥,以授权将Git推送到Github。

最终,在最终放弃那条路线之后,我确实意识到自己错了,并且Dokku应该工作的大致方式是 localhost-> Git push-> Dokku->部署

在开发的最后15到18个小时内都处于这种状态。该流程似乎可以正常工作,我实际上并不需要在DigitalOcean Droplet上进行大量安装。

但是,我面临的最大问题是


我当前的安装情况如下:

  • DigitalOcean Droplet 1单击Dokku版本0.14.6
  • 操作系统Ubuntu 18.04

我已将所有建议的文件添加到根文件夹下的以下在线博客和论坛中:


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'),
    }

}

我期望在输入“ git push dokku master ”后,编译将为我提供一个部署URL,我可以查看并启动Django服务器。

但是,我当时所面对的是:



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



更具体地说:

  

django.core.exceptions.ImproperlyConfigured:加载错误   psycopg2模块:没有名为'psycopg2'“的模块错误,一直存在   无论我如何尝试解决该错误,该问题都将持续存在。 [远程拒绝]主机->主机(预接收钩子   拒绝)错误:无法将某些引用推送到“ dokku @ server”


无论我如何尝试在Dokku和localhost上安装“ psycopg2”,我都无法解决该错误。

我衷心希望有人能为我提供上述帮助,因为我对Dokku或其真正如何编译软件包以及为何编译器无法检索“ psycopg2”的部署都不熟悉。

我也希望这不是前进的唯一问题。

希望听听您的专家建议。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用此docker命令安装psycopg2适配器

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

如果愿意,您还可以提及自己的特定版本。

如果仍然出现错误,则表示容器未正确停止。使用以下命令将其停止

docker-compose down

然后在安装psycopg2之后启动它

docker-compose up