设置环境变量 [英] Set an environment variable

查看:118
本文介绍了设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自一个甚至没有新手的另一个问题:


在Unix中你可以用命令设置一个环境变量

export PYTHONPATH

但是我想从.py脚本设置变量。


所以我的问题是:

如何在一个环境变量中导出.py脚本?

谢谢


克里斯

Another question from a not even newbie:

In Unix you can set an environment variable with the command
export PYTHONPATH
but I would like to set the variable from at .py script.

So my question is:
How do I export an environment variable in a .py script?
Thanks

Chris

推荐答案

只需使用

os.system(" export PYTHONPATH =%s"%(" your_pythonpath"))

Just use
os.system("export PYTHONPATH = %s" %("your_pythonpath"))


在Unix中,你通常不会影响你的父程序的环境(广义上的广告,包括环境变量,当前工作目录,

打开的文件,有效的用户ID等)。


你有两个基本选择来实现这样的效果。首先,您可以使用所需的环境启动子shell,例如(未经测试)

os.environ [''VARIABLE''] =''value''< br $>
os.system(os.environ [''shell''])

(像''su'这样的命令以这种方式工作)

>
其次,您可以生成shell命令,这些命令可以在执行时更改

shell的环境。类似于:

print" VARIABLE = value"

要使用此功能,用户必须编写类似

eval`myscript.py`的内容

而不仅仅是

myscript.py

这通常可以封装在shell别名或函数中。

(像''resize''这样的命令(它设置了shell对终端大小的看法)工作

这样的方式)


最后,您可以使用特定于操作系统的界面,如linux''ptrace

来更改调用进程内存的实际内容。我没有立即找到一个这样的例子,但在Linux上它将包括使用

PTRACE_PEEKDATA和PTRACE_POKEDATA来定位另一个环境

处理并修改它。 ptrace接口不能从任何标准的
Python模块中获得,并且无论如何都不可移植。 (虽然我的manpage实际上说

''符合SVr4,......,X / OPEN,BSD 4.3''所以也许ptrace更便携

比我认为的那样)


杰夫


-----开始PGP签名-----

版本:GnuPG v1 .4.1(GNU / Linux)

iD8DBQFDV4sxJd01MZaTXX0RAj4aAJ41BlvbrZxLNrhm + Ea5bg MyZOkVwQCglsLw

wFB6iqRqw3FRznozCpV5qh0 =

= MqJa

-----结束PGP SIGNATURE -----

In Unix, you generally can''t affect the environment of your parent program (in
a broad sense, which includes environment variables, current working directory,
opened files, effective user ID, etc).

You have two basic choices to achieve an effect like this. First, you can
start a subshell with the desired environment, something like (untested)
os.environ[''VARIABLE''] = ''value''
os.system(os.environ[''shell''])
(commands like ''su'' work in this way)

Second, you can produce shell commands that have the effect of changing a
shell''s environment when they are executed. Something like:
print "VARIABLE=value"
To use this, the user must write something like
eval `myscript.py`
instead of just
myscript.py
this can typically be encapsulated in a shell alias or function.
(commands like ''resize'' (which sets the shell''s idea of a terminal''s size) work
this way)

Finally, you might be able to use an OS-specific interface like linux'' ptrace
to change the actual contents of a calling process''s memory. I didn''t
immediately find an example of this, but on Linux it would consist of using
PTRACE_PEEKDATA and PTRACE_POKEDATA to locate the environment in another
process and modify it. The ptrace interface is not available from any standard
Python module, and isn''t portable anyway. (though my manpage actually says
''CONFORMING TO SVr4, ..., X/OPEN, BSD 4.3'' so maybe ptrace is more portable
than I believed)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDV4sxJd01MZaTXX0RAj4aAJ41BlvbrZxLNrhm+Ea5bg MyZOkVwQCglsLw
wFB6iqRqw3FRznozCpV5qh0=
=MqJa
-----END PGP SIGNATURE-----


谢谢杰夫和疯狂的88.
Thanks Jeff and the crazy 88.


这篇关于设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆