当我从网络运行 CGI 脚本时,为什么 python 找不到某些模块?

发布于 2024-09-24 13:49:50 字数 227 浏览 0 评论 0原文

我不知道这里可能出现什么问题:

我有一些来自 Biopython 的模块,当使用交互式提示或通过命令行执行 python 脚本时,我可以轻松导入这些模块。

问题是,当我尝试在 Web 可执行 cgi 脚本中导入相同的 biopython 模块时,我收到“导入错误”

:没有 名为 Bio 的模块

这里有什么想法吗?

I have no idea what could be the problem here:

I have some modules from Biopython which I can import easily when using the interactive prompt or executing python scripts via the command-line.

The problem is, when I try and import the same biopython modules in a web-executable cgi script, I get a "Import Error"

: No
module named Bio

Any ideas here?

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

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

发布评论

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

评论(3

锦欢 2024-10-01 13:49:50

以下是几种可能性:

  • Apache(在 Unix 上)通常以不同的用户身份、不同的环境运行,从命令行运行 python。尝试制作一个仅打印 sys.versionsys.prefix 的小脚本,并通过 apache 和命令行比较结果,以确保您是在两个环境中从相同的 python 安装运行。
  • Biopython 是安装在您的主目录下,还是仅对您的普通用户可读?同样,由于 apache 通常以不同的用户身份运行,因此您可能无权访问该位置,因此无法导入它。
  • 在尝试导入 Biopython 之前,您可以尝试执行 import site 吗?当您通过 apache 运行时,可能有些东西阻止导入站点包。

Here are a couple of possibilities:

  • Apache (on Unix) generally runs as a different user, and with a different environment, to python from the command line. Try making a small script that just prints out sys.version and sys.prefix, and compare the result through apache and via the command line, to make sure that you're running from the same installation of python in both environments.
  • Is Biopython installed under your home directory, or only readable just for your normal user? Again, because apache generally runs as a different user, perhaps you don't have access to that location, so can't import it.
  • Can you try doing import site before trying to import Biopython? Perhaps something is preventing site packages from being imported when you run through apache.
岁吢 2024-10-01 13:49:50

在 cgi 脚本中,您可以尝试在导入之前添加此包的路径。

sys.path.insert(0, 'path to biopython package')

如果您使用 Apache,您应该能够使用指令 SetEnv 在 conf 文件中设置 PYTHONPATH

SetEnv PYTHONPATH "path to biopython package"

In the cgi script, you could try to add the path to this package before any import.

sys.path.insert(0, 'path to biopython package')

If you are using Apache, you should be able to set the PYTHONPATH in conf file with directive SetEnv

SetEnv PYTHONPATH "path to biopython package"
傲性难收 2024-10-01 13:49:50

我有同样的问题。我通过在终端中使用命令更改 Linux Ubuntu 中的 Apache 用户解决了这个问题:

sudo gedit /etc/apache2/envvars

请更改 export APACHE_RUN_USERexport APACHE_RUN_GROUP 上的 www-data code> 到您当前的用户或可以运行 python 脚本的用户。
玩的很开心 ;)

I had same problem. I solved this problem by changing user of Apache in Linux Ubuntu by command in terminal:

sudo gedit /etc/apache2/envvars

Please change www-data on export APACHE_RUN_USER and export APACHE_RUN_GROUP to your current user or user that can run python script.
Have a good time ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文