apache Python + CGI 脚本无法访问环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python + CGI script cannot access environment variables
提问by ametade
I'm coding a webservice on python that uses an Oracle database. I have cx_Oracle installed and working but I'm having some problems when I run my python code as CGI using Apache.
我在使用 Oracle 数据库的 python 上编写 web 服务。我已经安装并运行了 cx_Oracle,但是当我使用 Apache 将 python 代码作为 CGI 运行时遇到了一些问题。
For example the following code works perfectly at the command line:
例如,以下代码在命令行中完美运行:
#!/usr/bin/python
import os
import cx_Oracle
import defs as df
os.putenv('ORACLE_HOME', '/oracledb/10.2.0/')
os.putenv('LD_LIBRARY_PATH', '/oracledb/10.2.0/lib')
con = cx_Oracle.Connection(df.DB_USER, df.DB_PASS, df.DB_SID)
print con
But when I run it as CGI I get a "cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle" at the apache error log.
但是当我将它作为 CGI 运行时,我在 apache 错误日志中收到“cx_Oracle.InterfaceError:无法获取 Oracle 环境句柄”。
I searched the Net and everybody says that I have to set the ORACLE_HOMEand LD_LIBRARY_PATHenvironment variables. Somehow the CGI script cannot access this environment variables even when I define them using os.putenvas you can see at the code.
我搜索网络和大家说,我要设置ORACLE_HOME和LD_LIBRARY_PATH环境变量。不知何故,CGI 脚本无法访问这个环境变量,即使我使用os.putenv代码定义它们也是如此。
What I'm I doing wrong? Thanks!
我做错了什么?谢谢!
采纳答案by ametade
I've managed to solve the problem.
我已经设法解决了这个问题。
Somehow the user and group that apache was using didn't have access to the environment variables. I solved the problem by changing the user and group that apache was using to a user that I was certain to have access to this variables.
不知何故,apache 使用的用户和组无权访问环境变量。我通过将 apache 使用的用户和组更改为我确定可以访问此变量的用户来解决该问题。
It's strange (and frustrating) that it's so difficult to set this variables using Python.
使用 Python 设置这个变量如此困难,这很奇怪(也令人沮丧)。
Thanks to everyone that answered my question!
感谢所有回答我问题的人!
回答by dmitry
This is working for me:
这对我有用:
os.putenv('ORACLE_HOME', '/oracle/client/v10.2.0.3-64bit')
os.putenv('LD_LIBRARY_PATH', '/oracle/client/v10.2.0.3-64bit/lib')
os.environ['ORACLE_HOME'] = '/oracle/client/v10.2.0.3-64bit'
os.environ['LD_LIBRARY_PATH'] = '/oracle/client/v10.2.0.3-64bit/lib'
Mind that first putenv, then update environ.
先记住putenv,再更新environ。
回答by RichieHindle
You need this:
你需要这个:
os.environ['ORACLE_HOME'] = '/oracledb/10.2.0/'
os.environ['LD_LIBRARY_PATH'] = '/oracledb/10.2.0/lib'
instead of using os.putenv()because os.putenv()doesn't update os.environ, which cx_Oracleis presumably looking at.
而不是使用os.putenv()因为os.putenv()不会更新os.environ,这cx_Oracle大概是在看。
Documentation: Miscellaneous operating system interfacessays: "Note: Calling putenv() directly does not change os.environ, so it's better to modify os.environ."
文档:杂项操作系统接口说:“注意:直接调用putenv()不会改变os.environ,所以最好修改os.environ。”
回答by ZiTAL
Don't forget adding envmodule for apache:
不要忘记为 apache添加env模块:
a2enmod env
in .htaccess or apache configuration:
在 .htaccess 或 apache 配置中:
SetEnv LD_LIBRARY_PATH /oracle_lib_path
in /etc/apache2/envvars doesn't work
在 /etc/apache2/envvars 中不起作用
回答by Mark Harrison
You can eliminate the problem altogether if you eliminate the need to set the environment variables. Here's a note on how to do this by installing the Oracle Instant Client on your box.
如果您不需要设置环境变量,则可以完全消除该问题。下面是有关如何通过在您的机器上安装 Oracle Instant Client 来执行此操作的说明。
installing Oracle Instantclient on Linux without setting environment variables?
回答by Cito
The actual question why the questioner's code did not work has not yet been answered.
为什么提问者的代码不起作用的实际问题尚未得到解答。
The answer is that the environment variable LD_LIBRARY_PATH is only evaluated when an application is started (in this case the Python interpreter). When Python has started, it is already too late to mess with this variable; and it doesn't matter at all whether you set it using os.environ or os.putenv (but generally the former should be used).
答案是环境变量 LD_LIBRARY_PATH 仅在应用程序启动时进行评估(在本例中为 Python 解释器)。当 Python 启动时,再去弄乱这个变量已经太晚了;并且无论您是使用 os.environ 还是 os.putenv 设置它都无关紧要(但通常应该使用前者)。
The solution is to set the LD_LIBRARY_PATH environment variable in a wrapper script that starts the Python script, or to start Apache with that environment variable already set. On OpenSUSE, you can do the latter by setting LD_LIBRARY_PATH in /etc/sysconfig/apache2, for instance.
解决方案是在启动 Python 脚本的包装器脚本中设置 LD_LIBRARY_PATH 环境变量,或者在已设置该环境变量的情况下启动 Apache。例如,在 OpenSUSE 上,您可以通过在 /etc/sysconfig/apache2 中设置 LD_LIBRARY_PATH 来完成后者。
By the way, the same problem exists when using mod_wsgi instead of a CGI script. See the section "Unable To Find Python Shared Library" on the mod_wsgi Installation Issuespage.
顺便说一下,使用 mod_wsgi 而不是 CGI 脚本时也存在同样的问题。请参阅 mod_wsgi安装问题页面上的“无法找到 Python 共享库”部分。
回答by rob
回答by Aaron Watters
You could use a shell script to implement the CGI, set the environment variables in the shell script and call the python script from the shell script.
您可以使用 shell 脚本来实现 CGI,在 shell 脚本中设置环境变量并从 shell 脚本调用 python 脚本。
Setting environment variables from within python seems to be a tricky thing, especially when you are dealing with how libraries are loaded...
在 python 中设置环境变量似乎是一件棘手的事情,尤其是当您处理库的加载方式时......
回答by eduffy
Are your statements out of order?
你的陈述有问题吗?
#!/usr/bin/python
import os
os.putenv('ORACLE_HOME', '/oracledb/10.2.0/')
os.putenv('LD_LIBRARY_PATH', '/oracledb/10.2.0/lib')
import cx_Oracle
import defs as df
con = cx_Oracle.Connection(df.DB_USER, df.DB_PASS, df.DB_SID)
print con

