小编Wil*_*son的帖子

找不到pg_config可执行文件

我在安装psycopg2时遇到问题.我尝试以下时出现以下错误pip install psycopg2:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:



    python setup.py build_ext --pg-config /path/to/pg_config build ...



or with the pg_config option in 'setup.cfg'.

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/psycopg2
Run Code Online (Sandbox Code Playgroud)

但问题pg_config实际上是在我的PATH; 它运行没有任何问题:

$ which pg_config
/usr/pgsql-9.1/bin/pg_config
Run Code Online (Sandbox Code Playgroud)

我尝试将pg_config路径添加到该setup.cfg文件并使用我从其网站(http://initd.org/psycopg/)下载的源文件构建它,我收到以下错误消息!

Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'
Run Code Online (Sandbox Code Playgroud)

但实际上它就是!!!

我对这些错误感到困惑.有人可以帮忙吗?

顺便说一句,我 …

python pip psycopg2

646
推荐指数
38
解决办法
43万
查看次数

如何在蓝图中而不是在请求中获取应用程序上下文?

我正在尝试将一组 Flask 应用程序转换为具有多个蓝图的单个应用程序。

在我的一个应用程序中,我有一个在后台定期运行的任务,与请求无关。它看起来像这样:

import apscheduler.schedulers.background
import flask

app = flask.Flask(__name__)
app.config['DATABASE']
scheduler = apscheduler.schedulers.background.BackgroundScheduler()
scheduler.start()

def db():
    _db = flask.g.get('_db')
    if _db is None:
        _db = get_db_connection_somehow(app.config['DATABASE'])
        flask.g._db = _db
    return _db

@scheduler.scheduled_job('interval', hours=1)
def do_a_thing():
    with app.app_context():
        db().do_a_thing()
Run Code Online (Sandbox Code Playgroud)

当我将此应用程序转换为蓝图时,我无法访问应用程序对象,并且无法弄清楚如何在需要时创建应用程序上下文。这是我尝试过的:

import apscheduler.schedulers.background
import flask

bp = flask.Blueprint('my_blueprint', __name__)
scheduler = apscheduler.schedulers.background.BackgroundScheduler()
scheduler.start()

def db():
    _db = flask.g.get('_db')
    if _db is None:
        _db = get_db_connection_somehow(flask.current_app.config['DATABASE'])
        flask.g._db = _db
    return _db

@bp.record
def record(state):
    with state.app.app_context():
        flask.g._app = state.app

@scheduler.scheduled_job('interval', hours=1) …
Run Code Online (Sandbox Code Playgroud)

python flask

6
推荐指数
1
解决办法
1567
查看次数

从一系列词典中返回一个特定的词典

如果我有以下代码

attributes = []

attributes.append({'attribute': 'noir', 'group': 'coloris', 'id': '8'})
attributes.append({'attribute': 's', 'group': 'taille_textile', 'id': '29'})
attributes.append({'attribute': 'm', 'group': 'taille_textile', 'id': '24'})
attributes.append({'attribute': 'l', 'group': 'taille_textile', 'id': '25'})
attributes.append({'attribute': 'xl', 'group': 'taille_textile', 'id': '26'})
Run Code Online (Sandbox Code Playgroud)

我想返回列表中包含某个id的对象,最好的方法是什么?

我知道一个解决方案是使用这样的for循环

def getItemById(id):
    for i in attributes:
        for k,v in i.items():
            if (k == 'id' and v == id):
                return i
Run Code Online (Sandbox Code Playgroud)

我敢肯定除此之外必须有更优雅或更有效的方法吗?

这里有机会使用lambdas吗?这会带来性能上的好处吗?

python lambda dictionary list object

2
推荐指数
1
解决办法
233
查看次数

标签 统计

python ×3

dictionary ×1

flask ×1

lambda ×1

list ×1

object ×1

pip ×1

psycopg2 ×1