可以公开从 pantsbuild 导入 pex 的模块吗?

Can a module import of a pex from pantsbuild be exposed?

来自 https://github.com/pantsbuild/pex/issues/1181 的交叉post 因为我真的不知道在哪里问这个问题。

我正在使用 Nginx Unit,它具有适用于我的 FastAPI 应用程序的 ASGI 配置。它工作正常,但我不知道如何将它与 PEX 一起使用。

我在下面附加了它,但重点是 Nginx 单元有一个 Python 插件,它正在寻找一个“模块”和一个“可调用”。

现在看起来像“apigateway.main:app”,类似于 uvicorn 或您正在使用的任何其他服务器实现。与 uvicorn、gunicorn 或其他任何工具不同——我认为我无法将任何其他工具打包到我的 pex 文件中并使用 PEX_SCRIPT.

有没有关于if/how我可以在 pex 之外公开模块和变量的建议?

{
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
    },
    "routes": [
        {
            "action": {
                "pass": "applications/api"
            }
        }
    ],
    "applications": {
        "api": {
            "type": "python 3.9",
            "path": "/app",
            "module": "apigateway.main",
            "callable": "app",
            "limits": {
                "requests": 100
            },
            "processes": {}
        }
    },
    "access_log": "/var/log/access.log"
}

这已在 Github 问题单 (https://github.com/pantsbuild/pex/issues/1181) 中得到回答:

All the traditional language here is meant to segue to the recent venv pex tool feature released with Pex 2.1.22. If you build your PEX file with venv support (add --include-tools to the Pex command line) then you gain the ability to create a venv from your PEX file. Doing so is a one-time manual step on the target machine or image:

PEX_TOOLS=1 ./my.pex venv /app That will create a virtual environment containing your app and its dependencies under /app. You can point Nginx Unit there with home. I've done all this over at https://github.com/jsirois/pex-issues-1181 to prove it works.