Run a python script with a dot slash. /

I installed two python 2.7.3 in my home directory

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 shell called "python" in / home / luban / bin to call another python when I work on different systems.

[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 check shell /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

 [ 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 might work:

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

After I changed the shebang line to #!/home/luban/Linux/Python/2.7.3/bin/python , use ./ to execute the script.

 [ 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,

but if I use the #!/home/luban/bin/python shell in my python script, use ./ to run the script, can't it work?

I had many scripts used by #!/home/luban/bin/python , when I just installed python under #!/home/luban/ for Linux, they can work with ./ , I don’t want to change them,

So, how ./ run a python script if I want the KEEP wrapper #!/home/luban/bin/python be a shebang line?


DEIT:

./myscript.py does NOT work with the wrapper #!/home/luban/bin/python under CentOS 5.4, Bash 3.2.25

Today I have a test under CentOS 6.4, Bash 4.1.2 :

I added

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

wrapper #!/home/luban/bin/python for tracking.

./myscript.py works with the wrapper #!/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 this could be a Bash 3.2.25 bug for ./ when I use the #!/home/luban/bin/python wrapper?

+4
source share
1 answer

Did you consider:

 #!/usr/bin/env python 

This usually works for me if python is in the search path.

In your case, you want your profile to have the following settings in the profile settings:

 # 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 is run using shebang "#! / Usr / bin / env python", it will find the correct python for you.

+4
source

All Articles