为什么要使用env设置一个初始环境阻止在Ubuntu上启动我的Python脚本? [英] why does setting an initial environment using env stall the launch of my Python script on Ubuntu?

查看:126
本文介绍了为什么要使用env设置一个初始环境阻止在Ubuntu上启动我的Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试脚本,例如test.py,我想让它在脚本开始之前执行一个特定的环境变量集:

 #/ usr / bin / env TEST =任何python 

打印你好,世界。

运行正常工作原理:

  $ python test.py 
你好,世界。

但是,如果我作为程序运行:

  $ chmod + x test.py 
$ ./test.py

字符串永远不会打印,而执行只是停顿,顶报告一个名为test.py的进程,该进程正在使用100%的CPU。



这只发生在我的Ubuntu机器上,似乎在OS X上很好。



原因是最终我想让一个特定的脚本总是运行在32位Python中设置:

 #!/ usr / bin / env VERSIONER_PYTHON_PREFER_32_BIT = yes python 

在文件的顶部。但是,如果这意味着该脚本将不会在Linux机器上执行,那么这是一个不进行的操作。我发现有一个类似的效果,无论调用什么指定的环境变量。但是,如果没有设置环境变量:

 #!/ usr / bin / env python 

打印你好,世界。

脚本运行正常:

  $ ./test.py 
你好,世界。

这是Python中的错误还是在 env ,或者我做错了?

解决方案

在Linux上,

 #!/ usr / bin / env TEST = any python 

通过 TEST =任何python 作为 env 的一个参数。
所以 env 不会正确处理参数。



底线是你只能放一个命令在 env 之后,其他所有内容最好被忽略。



Shebang上的维基百科条目


另一个可移植性问题是命令
参数的解释。一些系统,包括Linux,不要拆分
参数[24]
;例如,当使用第一个
行运行脚本时,如



#!/ usr / bin / env python -c



也就是说,python -c将作为一个参数传递给/ usr / bin / env,
而不是两个参数。 Cygwin也以这种方式行事。



I have a test script, e.g. "test.py", and I want to make it so that it executes with a particular environment variable set before the script begins:

#!/usr/bin/env TEST=anything python

print "Hello, world."

Running this normally works as expected:

$ python test.py 
Hello, world.

However, if I run it as a program:

$ chmod +x test.py
$ ./test.py 

The string is never printed, instead the execution just stalls and "top" reports a process called "test.py" which is using 100% CPU.

This only happens on my Ubuntu machine, seems to be fine on OS X.

The reason is that eventually I want to make a particular script always run in a 32-bit Python by setting:

#!/usr/bin/env VERSIONER_PYTHON_PREFER_32_BIT=yes python

at the top of the file. However this is a no-go if it means the script won't execute on Linux machines. I found there is a similar effect no matter what the specified environment variable is called. However, if there is no environment variable set:

#!/usr/bin/env python

print "Hello, world."

The script runs just fine:

$ ./test.py 
Hello, world.

Is this a bug in Python or in env, or am I doing something wrong?

解决方案

On Linux,

#!/usr/bin/env TEST=anything python

passes TEST=anything python as one argument to env. So env will not process the argument properly.

The bottom line is you can only put one command after env on the shebang line, all else will at best be ignored.

From the Wikipedia entry on Shebang:

Another portability problem is the interpretation of the command arguments. Some systems, including Linux, do not split up the arguments[24]; for example, when running the script with the first line like,

#!/usr/bin/env python -c

That is, python -c will be passed as one argument to /usr/bin/env, rather than two arguments. Cygwin also behaves this way.

这篇关于为什么要使用env设置一个初始环境阻止在Ubuntu上启动我的Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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