Run python script with dot slash ./ -


i installed 2 python 2.7.3 home directory

   1 linux:           /home/luban/linux/python/2.7.3    solaris:   /home/luban/sunos/python/2.7.3 

then create wrapper named "python" in /home/luban/bin call different python when working 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} not available ${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 test wrapper /home/luban/bin/python

[luban@lunbanworks 3] ~ > cat myscript.py      #!/home/luban/bin/python      myname="lunban"      print "myname %s" % myname  [luban@lunbanworks 4] chmod +x myscript.py 

i want use ./ run myscript.py

[luban@lunbanworks 5] ~ >./myscript.py      myname=luban: command not found.     lpr: unable access "myname" - no such file or directory 

use /home/luban/bin/python myscript.py can work:

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

after change shebang line #!/home/luban/linux/python/2.7.3/bin/python, use ./ can execute script.

[luban@lunbanworks 6] ~ >cat myscript.py      #!/home/luban/linux/python/2.7.3/bin/python     myname="lunban"      print "myname %s" % myname  [luban@lunbanworks 7] ~ >./myscript.py      myname luban 

why when use #!/home/luban/linux/python/2.7.3/bin/python @ beginning of myscript.py, ./myscript.py can work,

but if use wrapper #!/home/luban/bin/python in python script, use ./ run script, cannot not work?

i had many scripts used #!/home/luban/bin/python when installed python under #!/home/luban/ linux, can run ./, don't want change them,

so, how let ./ run python script if want keep wrapper #!/home/luban/bin/python shebang line?


deit:

./myscript.py can not work wrapper #!/home/luban/bin/python under centos 5.4, bash 3.2.25

today, have test under centos 6.4, bash 4.1.2:

i added

echo '$0 =' $0 echo '${command_path}/${command} ${1+"$@"} =' ${command_path}/${command} ${1+"$@"} 

into wrapper #!/home/luban/bin/python track.

./myscript.py works 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 luban 

so suppose may bash 3.2.25 bug ./ when use wrapper #!/home/luban/bin/python?

have considered:

#!/usr/bin/env python 

that works me, long python found in search path.

in case, want profile have following in profile setup:

# adjust preferred shell export path=/home/luban/linux/python/2.7.3:/home/luban/sunos/python/2.7.3:$path 

then, when python script runs "#!/usr/bin/env python" shebang, find right python you.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -