NotperlyConfigured:在GCP上加载psycopg2模块时出错

时间:2017-05-25 14:54:54

标签: python django postgresql google-app-engine

目前我正在尝试按照以下文档将我的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

上的数据库设置
# [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]

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

2 个答案:

答案 0 :(得分: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/.*$

答案 1 :(得分:0)

就我而言,它是通过pip install psycopg2

修复的