Python Azure上的Flask Postgres客户端:没有名为';psycopg2';但它已经安装好了

Python Azure上的Flask Postgres客户端:没有名为';psycopg2';但它已经安装好了,python,postgresql,azure,flask,Python,Postgresql,Azure,Flask,我已经构建了一个Flask应用程序,它连接Azure PostgreSQL数据库,并将数据从API上传到该数据库。在我的电脑上一切正常,但当我打开网页时,我发现一个错误: 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: Trac

我已经构建了一个Flask应用程序,它连接Azure PostgreSQL数据库,并将数据从API上传到该数据库。在我的电脑上一切正常,但当我打开网页时,我发现一个错误:

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:\Python34\Scripts\wfastcgi.py", line 711, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
    return env, get_wsgi_handler(handler_name)
  File "D:\Python34\Scripts\wfastcgi.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:\home\site\wwwroot\main.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
部署日志:

Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling python deployment.
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
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:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): requests==2.17.3 in d:\home\site\wwwroot\env\lib\site-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:\home\site\wwwroot\env\lib\site-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
在Kudu上的我的文件夹D:\Python34\Scripts>中,我得到以下异常:

Exception:
Traceback (most recent call last):
  File "D:\Python34\lib\shutil.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:\home\site\wwwroot\env\Scripts>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:\home\site\wwwroot\env\lib\site-packages
Cleaning up...

根据有关的官方文件,其对本案原因解释如下

在Azure上运行时,某些软件包可能不会使用pip进行安装。可能只是因为包在Python包索引中不可用。可能需要编译器(Azure应用程序服务中运行web应用程序的计算机上没有编译器)

因此,处理这个问题的解决方案显示在文档部分的解释中

  • 在项目上创建一个目录
    wheelhouse
  • 从下载
    psycopg
    程序包车轮,并将其放入
    wheelhouse
  • 编辑包的
    requirements.txt
    文件,如下所示,然后将项目重新部署到Azure

    --find-links wheelhouse
    psycopg2==2.7.1
    
  • 或者,您也可以将控制盘文件上载到Web应用程序的路径
    wwwroot
    ,并通过Kudu控制台运行命令
    pip install psycopy2‑2.7.1‑cp27‑cp27m‑win32.whl
    https://.scm.azurewebsites.net/DebugConsole

    --find-links wheelhouse
    psycopg2==2.7.1