Azure 上的 Flask Postgres 客户端:没有名为 'psycopg2' 的模块,但已安装



我已经构建了一个与Azure PostgreSQL数据库连接的烧瓶应用程序,并将数据从API上传到该数据库。一切都在我的PC上正常工作,但是当我打开网页时,我会收到一个错误:

The page cannot be displayed because an internal server error has occurred.

更深入地挖掘:

StdErr: 
2017-05-30 16:15:48.465025: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:Python34Scriptswfastcgi.py", line 711, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:Python34Scriptswfastcgi.py", line 568, in read_wsgi_handler
    return env, get_wsgi_handler(handler_name)
  File "D:Python34Scriptswfastcgi.py", line 541, in get_wsgi_handler
    handler = handler()
  File ".virtualenv_proxy.py", line 93, in get_venv_handler
    handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER'))
  File ".virtualenv_proxy.py", line 62, in get_wsgi_handler
    raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "main.app" could not be imported: Traceback (most recent call last):
  File ".virtualenv_proxy.py", line 46, in get_wsgi_handler
    handler = __import__(module_name, fromlist=[name_list[0][0]])
  File "D:homesitewwwrootmain.py", line 8, in <module>
    import psycopg2
ImportError: No module named 'psycopg2'
2017-05-30 16:15:48.480649: wfastcgi.py 2.1.1 closed

当我使用常规的psycopg2软件包时,即使在Azure部署期间,我也会出错。将我的requirements.txt更改为此之后,部署成功了,但是出现了所指定的问题。

Flask==0.12.1
requests==2.17.3
git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2

部署日志:

Command: "D:homesitedeploymentstoolsdeploy.cmd"
Handling python deployment.
KuduSync.NET from: 'D:homesiterepository' to: 'D:homesitewwwroot'
Copying file: 'requirements.txt'
Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
Detecting Python runtime from site configuration
Detected python-3.4
Found compatible virtual environment.
Pip install requirements.
Requirement already satisfied (use --upgrade to upgrade): Flask==0.12.1 in d:homesitewwwrootenvlibsite-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): requests==2.17.3 in d:homesitewwwrootenvlibsite-packages (from -r requirements.txt (line 2))
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:homesitewwwrootenvlibsite-packages (from -r requirements.txt (line 3))
Cleaning up...
Overwriting web.config with web.3.4.config
        1 file(s) copied.
Finished successfully.

我文件的顶部main.py

#!/usr/bin/env python
import requests
import csv
from os import listdir
from os.path import isfile, join, splitext
from datetime import datetime
import time
import psycopg2
from flask import Flask
app = Flask(__name__)
[...]

我尝试运行命令

pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2

在我的文件夹d: python34 scripts>上,我得到以下例外:

Exception:
Traceback (most recent call last):
  File "D:Python34libshutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'D:\local\Temp\pip_build_RD00155D83D9EE$\psycopg2\.git\objects\pack\pack-31a2b4dba04af9763e01b5b017870e20de3710c3.idx'

但是它在我的env文件夹中成功:

D:homesitewwwrootenvScripts>pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:homesitewwwrootenvlibsite-packages
Cleaning up...

根据有关Troubleshooting - Package Installation的官方文件,它解释了以下情况。

在Azure上运行时,某些软件包可能不会使用PIP安装。可能只是该软件包在Python软件包索引上不可用。可能是需要编译器(在Azure应用程序中运行Web应用程序的机器上没有编译器(。

因此,向文档部分的解释显示了处理此问题的解决方案。

  1. 在您的项目上创建一个目录wheelhouse
  2. 从这里下载psycopg软件包轮,然后将其放在wheelhouse
  3. 编辑以下包装的requirements.txt文件,然后将您的项目重新删除到Azure。

    --find-links wheelhouse
    psycopg2==2.7.1
    

或者您还可以将车轮文件上传到WebApp的路径wwwroot,并通过Kudu Console https://<your webapp name>.scm.azurewebsites.net/DebugConsole运行命令pip install psycopy2‑2.7.1‑cp27‑cp27m‑win32.whl

相关内容

最新更新