简体   繁体   English

用点斜杠运行python脚本./

[英]Run python script with dot slash ./

I installed two python 2.7.3 into my home directory 我在我的主目录中安装了两个python 2.7.3

   one is for Linux:           /home/luban/Linux/Python/2.7.3
   another is for Solaris:   /home/luban/SunOS/Python/2.7.3

then I create a wrapper named "python" in /home/luban/bin to call the different python when I am working on different systems. 然后我在/ home / luban / bin中创建一个名为“python”的包装器,当我在不同的系统上工作时调用不同的python。

[luban@lunbanworks 1] ~ > cat /home/luban/bin/python [luban @ lunbanworks 1]〜> cat / home / luban / bin / python

#!/bin/sh

CMD=`basename $0`

OS=`uname -s`


CMD_PATH="/home/luban/$OS/Python/2.7.3/bin"


if [ -x "${CMD_PATH}/${CMD}" ];then

    export PATH="${CMD_PATH}:${PATH}"

    exec ${CMD_PATH}/${CMD} ${1+"$@"}

else

    echo "${CMD} is not available for ${OS}" 1>&2

exit 1

fi

[luban@lunbanworks 2] `ls -l /home/luban/bin/python`

-rwxrwxr-x  1 luban lunban  221 Apr  5 19:11 python*

I use below script to test the wrapper /home/luban/bin/python 我使用下面的脚本来测试包装器/home/luban/bin/python

[luban@lunbanworks 3] ~ > cat myscript.py

    #!/home/luban/bin/python

    myname="lunban"

    print "myname is %s" % myname

[luban@lunbanworks 4] chmod +x myscript.py

I want to use ./ run myscript.py 我想使用./ run myscript.py

[luban@lunbanworks 5] ~ >./myscript.py

    myname=luban: Command not found.
    lpr: Unable to access "myname" - No such file or directory

use /home/luban/bin/python myscript.py can work: 使用/home/luban/bin/python myscript.py可以工作:

[luban@lunbanworks 5] ~ > `/home/luban/bin/python myscript.py`

    myname is luban

After I change the shebang line to #!/home/luban/Linux/Python/2.7.3/bin/python , use ./ can execute the script. 将shebang行更改为#!/home/luban/Linux/Python/2.7.3/bin/python ,使用./可以执行脚本。

[luban@lunbanworks 6] ~ >cat myscript.py

    #!/home/luban/Linux/Python/2.7.3/bin/python
    myname="lunban"

    print "myname is %s" % myname

[luban@lunbanworks 7] ~ >./myscript.py

    myname is luban

Why when I use #!/home/luban/Linux/Python/2.7.3/bin/python at the beginning of myscript.py, ./myscript.py can work, 为什么当我使用#!/home/luban/Linux/Python/2.7.3/bin/python在myscript.py年初, ./myscript.py可以工作,

but if I use the wrapper #!/home/luban/bin/python in my python script, use ./ to run the script, it cannot not work? 但是如果我在我的python脚本中使用包装器#!/home/luban/bin/python ,使用./来运行脚本,它不能工作吗?

I had many scripts used #!/home/luban/bin/python when I only installed python under #!/home/luban/ for Linux, they can run with ./ , I don't want to change them, 当我只在#!/home/luban/ for Linux下安装#!/home/luban/bin/python时,我有很多脚本#!/home/luban/bin/python ,它们可以用./运行,我不想改变它们,

so, how to let ./ run the python script If I want to KEEP wrapper #!/home/luban/bin/python as the shebang line? 所以,如何让./运行python脚本如果我想保持包装#!/home/luban/bin/python作为shebang线?


DEIT: DEIT:

./myscript.py can NOT work with the wrapper #!/home/luban/bin/python under CentOS 5.4, Bash 3.2.25 ./myscript.py无法使用CentOS 5.4, Bash 3.2.25下的包装器#!/home/luban/bin/python

today, I have a test under CentOS 6.4, Bash 4.1.2 : 今天,我在CentOS 6.4, Bash 4.1.2下进行了测试:

I added 我补充道

echo '$0 =' $0
echo '${COMMAND_PATH}/${COMMAND} ${1+"$@"} =' ${COMMAND_PATH}/${COMMAND} ${1+"$@"}

into the wrapper #!/home/luban/bin/python to track. 进入包装器#!/home/luban/bin/python进行跟踪。

./myscript.py works with the wrapper #!/home/luban/bin/python ./myscript.py与包装器#!/home/luban/bin/python

[luban@lunbanworks 20] ./myscript.py 
      $0 =  /home/luban/bin/python
      ${COMMAND_PATH}/${COMMAND} ${1+"$@"} =  /home/luban/Linux/Python/2.7.3/bin/python ./myscript.py
      myname is luban

So I suppose that it may be a Bash 3.2.25 bug for ./ when I use the wrapper #!/home/luban/bin/python ? 所以我想当我使用包装器#!/home/luban/bin/python时,它可能是./的Bash 3.2.25错误?

Have you considered: 你有没有考虑过:

#!/usr/bin/env python

That generally works for me, so long as python is found in the search path. 只要在搜索路径中找到python,这通常对我有用。

In your case, you would want your profile to have the following in the profile setup: 在您的情况下,您希望您的个人资料在个人资料设置中具有以下内容:

# Adjust for your preferred shell
export PATH=/home/luban/Linux/Python/2.7.3:/home/luban/SunOS/Python/2.7.3:$PATH

Then, when your python script runs with the "#!/usr/bin/env python" shebang, it will find the right python for you. 然后,当你的python脚本以“#!/ usr / bin / env python”shebang运行时,它会为你找到合适的python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM