Python Egg 可以交互方式找到,但在 fastcgi 中没有

发布于 2024-08-03 19:24:17 字数 307 浏览 8 评论 0原文

同意这个问题,及其答案。我添加了鸡蛋的路径并且它起作用了。但是,当我以交互方式运行 python 并导入 flup 时,它可以正常工作,没有任何问题或额外的路径规范。差别在哪里?

编辑:似乎在执行 fastcgi 操作时,.pth 文件没有被解析,但这只是一个猜测。需要更多官方说法。

In agreement to this question, and its answer. I added the path of the egg and it worked. However, when I run python interactively and I import flup, it works without any problem or additional path specification. Where is the difference ?

Edit: It appears that while doing fastcgi stuff, the .pth files are not parsed, but this is only a guess. Need more official statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-08-10 19:24:17

经过更彻底的分析后,我想我明白这里发生了什么。

当Python启动时,它会设置sys.path(所有这些都是初始化解释器的一部分)。

此时,环境用于确定在哪里可以找到.pth文件。如果此时没有定义 PYTHONPATH,那么它将找不到安装到 sys.prefix 的模块。此外,由于 easy-install.pth 可能安装到您的自定义前缀目录中,因此它不会找到要解析的 .pth 文件。

在解释器初始化后向 os.environ 或 sys.path 添加环境变量不会导致 .pth 文件被再次解析。这就是为什么您发现自己被迫手动执行您期望 Python 自然执行的操作。

我认为正确的解决方案是确保自定义路径在解释器启动时(即在执行 mysite.fcgi 之前)可供 Python 解释器使用。

我寻找将 PYTHONPATH 环境变量添加到 mod_fastcgi 的选项,但我没有看到这样的选项。也许这是一个通用的 Apache 选项,因此没有记录在 mod_fastcgi 中,或者可能无法在 mod_fastcgi 配置中设置静态变量。

鉴于此,我相信您可以通过以下方式产生解决方法:

  1. 创建一个包装脚本(shell 脚本或另一个 Python 脚本),它将作为您的新 FastCGI 处理程序。
  2. 在此包装器脚本中,将 PYTHONPATH 环境变量设置为您的前缀路径(就像您在用户环境中设置的那样)。
  3. 让包装器脚本使用更改后的环境为原始 fastcgi 处理程序 (mysite.fcgi) 启动 Python 进程。

虽然我没有一个好的测试环境,但我认为包装 shell 脚本看起来像这样:

#!/bin/sh
export PYTHONPATH=/your/local/python/path
/path/to/python /path/to/your/fastcgi/handler  # this line should be similar to what was supplied to mod_fastcgi originally

可能还有其他解决方法需要考虑。

  • 从 mysite.fgci,使 Python 根据新的、更改的环境重新处理 sys.path。我不知道这是如何完成的,但这可能比使用包装脚本更干净。
  • 找到一个 Apache/mod_fastcgi 选项,该选项允许为 fastcgi 进程指定环境变量 (PYTHONPATH)。

After some more thorough analysis, I think I understand what's going on here.

When Python starts up, it sets up the sys.path (all as part of initializing the interpreter).

At this time, the environment is used to determine where to find .pth files. If no PYTHONPATH is defined at this time, then it won't find your modules installed to sys.prefix. Additionally, since easy-install.pth is probably installed to your custom prefix directory, it won't find that .pth file to be parsed.

Adding environment variables to os.environ or sys.path after the interpreter has initialized won't cause .pth files to be parsed again. This is why you're finding yourself forced to manually do what you expect Python to do naturally.

I think the correct solution is to make sure the custom path is available to the Python interpreter at the time the interpreter starts up (which is before mysite.fcgi is executed).

I looked for options to add a PYTHONPATH environment variable to mod_fastcgi, but I see no such option. Perhaps it's a general Apache option, so not documented in mod_fastcgi, or perhaps it's not possible to set a static variable in the mod_fastcgi config.

Given that, I believe you could produce a workaround with the following:

  1. Create a wrapper script (a shell script or another Python script) that will be your new FastCGI handler.
  2. In this wrapper script, set the PYTHONPATH environment variable to your prefix path (just as you have set in your user environment which works).
  3. Have the wrapper script launch the Python process for your original fastcgi handler (mysite.fcgi) with the altered environment.

Although I don't have a good environment in which to test, I think a wrapper shell script would look something like:

#!/bin/sh
export PYTHONPATH=/your/local/python/path
/path/to/python /path/to/your/fastcgi/handler  # this line should be similar to what was supplied to mod_fastcgi originally

There may be alternative workarounds to consider.

  • From mysite.fgci, cause the Python to re-process the sys.path based on a new, altered environment. I don't know how this would be done, but this might be cleaner than having a wrapper script.
  • Find an Apache/mod_fastcgi option that allows an environment variable (PYTHONPATH) to be specified to the fastcgi process.
冷月断魂刀 2024-08-10 19:24:17

与交互使用的环境相比,Web 服务器运行的程序或代码运行的环境受到限制。最有可能的是,这种差异源于您的交互环境和 FastCGI 环境之间的差异。我无法告诉您的是在这种情况下哪个差异是至关重要的。

Programs run by or code run in a web server has a restricted environment compared with what you use interactively. Most likely, the difference stems from the difference between your interactive environment and the FastCGI environment. What I can't tell you is which difference is critical in this context.

冷…雨湿花 2024-08-10 19:24:17

我在 IIS (Windows) 下运行 Python 应用程序时遇到了类似的问题。我发现在 ISAPI 下运行时,eggs 不可读,因为 setuptools 会破坏压缩 Egg 上的权限,并且 ISAPI 应用程序在有限权限帐户下运行。

您可能在 FastCGI 中遇到同样的情况。如果 FastCGI 进程没有权限读取 Egg 或根据需要扩展 Egg,则可能会遇到问题。另外,我发现将 PYTHON_EGG_CACHE 环境变量设置为进程可写的目录对于某些 Egg 也是必要的(特别是具有二进制/扩展模块或必须作为文件访问的资源的 Egg)。

I've encountered a similar issue when running my Python applications under IIS (Windows). I've found that when running under ISAPI, eggs aren't readable because setuptools munges the permissions on zipped eggs, and because the ISAPI application runs under a limited privilege account.

You may be experiencing the same situation in FastCGI. If the FastCGI process doesn't have permission to read the eggs or expand the eggs as necessary, you may have problems. Also, I've found that setting the PYTHON_EGG_CACHE environment variable to a directory that's writable by the process is also necessary for some eggs (in particular, eggs with binary/extension modules or resources that must be accessed as files).

心清如水 2024-08-10 19:24:17

我同意,即使定义了 PYTHONPATH 环境变量,当 python 在 FastCGI 环境中启动时,.pth 文件也不会被解释。我现在不知道为什么会这样,但我确实有一个解决方法的建议。

使用 site.addsitedir。它将解释 .pth 文件,然后您可以简单地通过名称导入 Egg,而无需添加每个 Egg 的完整路径。

#!/user/bin/python2.6

import site

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/home/mhanney/.local/lib/python2.6/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/home/mhanney/.local/egg-cache'

没有必要使用虚拟环境。在我的共享托管提供商中,我只是使用以下命令在 ~/.local 中安装 Eggs。

python setup.py install --prefix=~/.local

这是 flup 'Hello World' 示例的变体,用于转储环境变量、路径和模块,这对于调试 FastCGI 非常有用。

#!/usr/bin/python2.6
import sys, os, site, StringIO
from pprint import pprint as p

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/home/mhanney/.local/lib/python2.6/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/home/mhanney/.local/egg-cache'

def test_app(environ, start_response):
    output = StringIO.StringIO()
    output.write("Environment:\n")
    for param in os.environ.keys():
        output.write("%s %s\n" % (param,os.environ[param]))
    output.write("\n\nsys.path:\n")
    p(sys.path, output)
    output.write("\n\nsys.modules:\n")
    p(sys.modules, output)
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield output.getvalue()

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    WSGIServer(test_app).run()

I concur that even with the PYTHONPATH environment variable defined the .pth files do not get interpreted when python starts up in the FastCGI environment. I do not now why this is so, but I do have a suggestion for a workaround.

Use site.addsitedir. It will interpret the .pth files allowing you to then import eggs simply by name without having to add the full path to each one.

#!/user/bin/python2.6

import site

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/home/mhanney/.local/lib/python2.6/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/home/mhanney/.local/egg-cache'

It is not necessary to use virtual env. At my shared hosting provider I just install eggs in ~/.local using

python setup.py install --prefix=~/.local

Here is a variation on the flup 'Hello World' example to dump the environment vars, path and modules, useful for debugging FastCGI.

#!/usr/bin/python2.6
import sys, os, site, StringIO
from pprint import pprint as p

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/home/mhanney/.local/lib/python2.6/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/home/mhanney/.local/egg-cache'

def test_app(environ, start_response):
    output = StringIO.StringIO()
    output.write("Environment:\n")
    for param in os.environ.keys():
        output.write("%s %s\n" % (param,os.environ[param]))
    output.write("\n\nsys.path:\n")
    p(sys.path, output)
    output.write("\n\nsys.modules:\n")
    p(sys.modules, output)
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield output.getvalue()

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    WSGIServer(test_app).run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文