从Web服务器执行时,为什么Python环境变量不同?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从Web服务器执行时,为什么Python环境变量不同?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3168字,纯文字阅读大概需要5分钟。
内容图文
![从Web服务器执行时,为什么Python环境变量不同?](/upload/InfoBanner/zyjiaocheng/696/6ef1a73ece134e5488e41befd33d0f75.jpg)
我有以下可执行python脚本:
#!/usr/bin/python
import os
print os.environ
当我以root身份从命令行执行它时,得到以下信息:
{
'LANG': 'en_US.UTF-8',
'TERM': 'xterm-256color',
'SHELL': '/bin/bash',
'LESSCLOSE': '/usr/bin/lesspipe %s %s',
'LANGUAGE': 'en_US:en',
'SHLVL': '1',
'SSH_TTY': '/dev/pts/0',
'OLDPWD': '/var/www/bais-mordechai-laravel',
'COMP_WORDBREAKS': ' \t\n"\'><;|&(:',
'PWD': '/var/www/bais-mordechai-laravel/public',
'LESSOPEN': '| /usr/bin/lesspipe %s',
'SSH_CLIENT': '71.205.188.8 56489 22',
'LOGNAME': 'root',
'USER': 'root',
'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games',
'MAIL': '/var/mail/root',
'LS_COLORS':'...',
'HOME': '/root',
'_': './pythontest',
'SSH_CONNECTION': '...'
}
但是,当我从php脚本(在nginx服务器上运行)运行此文件并回显结果时,我得到:
{
'HOME': '/var/www',
'PWD': '/var/www/bais-mordechai-laravel/public',
'USER': 'www-data'
}
他们为什么不同?具体来说,如何获取包含“ PATH”变量的网络版本?
解决方法:
对于Apache,请参阅mod_setenv.
This module allows for control of internal environment variables that are used by various Apache HTTP Server modules. These variables are also provided to CGI scripts as native system environment variables, and available for use in SSI pages. Environment variables may be passed from the shell which invoked the httpd process. Alternatively, environment variables may be set or unset within the configuration process.
如果您使用的是Apache的FCGId,请参见FcgidInitialEnv.
Use FcgidInitialEnv to define environment variables to pass to the FastCGI application. This directive can be used multiple times.
This setting will apply to all applications spawned for this server or virtual host. Use FcgidCmdOptions to apply this setting to a single application.
对于Nginx CGI,请参阅env设置.
By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables.
如果使用的是NgxWSGIModule,请参见wsgi_var设置.
Directive assigns the variable, which will be added to the
environment dictionary passed to the WSGI application.
It is possible to use strings, nginx variables and their
combination as values. Directives not set are inherited from the
outer level.
如果在(gunicorn | tornado | twisted | etc)监督下运行应用程序,请参见environment指令.
A list of key/value pairs in the form KEY=”val”,KEY2=”val2″ that will be placed in the supervisord process’ environment (and as a result in all of its child process’ environments). This option can include the value %(here)s, which expands to the directory in which the supervisord configuration file was found. Values containing non-alphanumeric characters should be quoted (e.g. KEY=”val:123″,KEY2=”val,456″). Otherwise, quoting the values is optional but recommended. Note that subprocesses will inherit the environment variables of the shell used to start supervisord except for the ones overridden here and within the program’s environment option.
如果您还有其他用途,请告诉我.
您始终可以使用os.environ注入环境变量-在(c | fc | ws)gi.py脚本的开头执行此操作.硒也sys.path:
sys.path.append('/usr/local/django')
内容总结
以上是互联网集市为您收集整理的从Web服务器执行时,为什么Python环境变量不同?全部内容,希望文章能够帮你解决从Web服务器执行时,为什么Python环境变量不同?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。