【问题标题】:ImproperlyConfigured: Error loading psycopg2 module on GCP配置不当:在 GCP 上加载 psycopg2 模块时出错
【发布时间】:2017-10-26 06:53:03
【问题描述】:

目前我正在尝试按照以下文档将我的 django 网络应用程序启动到 GCP: here

仅供参考:我使用的是 Django 1.9 和 Python 2.7

使用gcloud app deploy 部署应用程序后,我检查了我的应用程序仪表板并看到以下错误:

1) ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2

2) RuntimeError: populate() isn't reentrant

在对如何修复 psycopg2 错误进行一些研究后,我找到了一些答案here

但这无济于事。

这是我的 requirements.txt 中的内容

psycopg2==2.7.1
Flask==0.12
Flask-SQLAlchemy==2.2
gunicorn==19.7.0
PyMySQL==0.7.10
django==1.9
django-imagekit
pillow
pyexcel
pyexcel_xls
django_excel
xlsxwriter
python-dateutil
django-mail-templated
djangorestframework
django-cors-headers
django-extra-fields
pytz
numpy
reportlab
xhtml2pdf
html5lib==1.0b8
pypdf

这就是我的 app.yaml 中的内容

runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static/
- url: .*
  script: root.wsgi.application

libraries:
- name: MySQLdb 
  version: 1.2.5
- name: django 
  version: 1.9

env_variables:
    # Replace user, password, database, and instance connection name with the values obtained
    # when configuring your Cloud SQL instance.
    SQLALCHEMY_DATABASE_URI: >-
      postgresql+psycopg2://postgres:db_password@/DATABASE?host=/cloudsql/db:location:instance

beta_settings:
    cloud_sql_instances:db:location:instance


skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- ^env/.*$

最后通过 settings.py 上的 db 设置

# [START db_setup]
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so connect to Google Cloud SQL using
    # the unix socket at /cloudsql/<your-cloudsql-connection string>
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'HOST': '/cloudsql/db_name:location:instance_name',
            'NAME': 'db_name',
            'USER': 'user_name',
            'PASSWORD': 'db_password',
        }
    }
else:
    from .local_settings import *
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': name,
            'USER': user,
            'PASSWORD': password,
            'HOST' : host,
            'PORT' : '',
        }
    }
    # [END db_setup]

非常感谢此问题的解决方案。谢谢。

【问题讨论】:

  • 您的requirements.txt 文件是什么样的?
  • @themanatuf 我已经编辑了我的帖子以包含我的 requirements.txt 和 app.yaml。
  • @themanatuf 我还添加了一个 appengine_config.py 但它给了我这个错误“ValueError: virtualenv: cannot access lib: No such virtualenv or site directory”

标签: python django postgresql google-app-engine


【解决方案1】:

所以我解决了关于我的 postgreSQL 的问题。事实证明,由于我使用的是标准 App Engine 环境,它不支持像 postgreSQL 这样的第三方应用程序。所以我不得不切换到非常简单的 Flex 环境。我刚刚将我的 app.yaml 更改为:

# [START runtime]
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT myproject.wsgi

beta_settings:
    cloud_sql_instances: project:location:instance_name

runtime_config:
  python_version: 2

# [END runtime]

# Google App Engine limits application deployments to 10,000 uploaded files per
# version. The skip_files section allows us to skip virtual environment files
# to meet this requirement. The first 5 are the default regular expressions to
# skip, while the last one is for all env/ files.
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- ^env/.*$

【讨论】:

    【解决方案2】:

    在我的情况下,它是通过执行 pip install psycopg2 修复的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-26
      • 2021-08-05
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多