简体   繁体   中英

Why does python script in current directory need ./ for execution with shebang?

In my current directory there is the executable script.py with the shebang #!/usr/bin/env python . To execute it in bash, I need to type ./script.py . From what I read about shebang I would expect script.py to execute the script too, same as python script.py does, but it doesn't. What is the difference between the two latter options?

On Un*x-like systems, . is not in the path for security reasons, in order to avoid executing a command like ls which could be in a user directory (this security breach is best exploited as root)

This is less concerning for python scripts than for classic commands like ls , but python scripts with a shebang are executables so the obey the general security rules.

When you type python myscript.py you know that the script is in the current directory, and python reads it from here, regardless of the system path (it's seen as data , not as executable , the real executable being python )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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