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,
passes
TEST=anything pythonas one argument toenv.So
envwill not process the argument properly.The bottom line is you can only put one command after
envon the shebang line, all else will at best be ignored.From the Wikipedia entry on Shebang: