0

I’m wondering if shebang is specified in the python script, does it overwrite the caller?

For example, let’s say I have a test.py,

#!/usr/bin/python3 print(“hello world”) 

And then I call it this way: /usr/bin/python2 test.py

Which python version will call the hello world print fiction?

1
  • The shebang is seen only by the shell executing the file; the Python interpreter ignores it as it is a comment. Commented Sep 29, 2020 at 14:12

1 Answer 1

5

No. It is only used if you make your python file executable and run directly

./test.py 

As it is being read and used by the shell attempting to run that file. By doing

/usr/bin/python2 test.py 

you in fact run /usr/bin/python2 interpreter, which is given test.py as argument. And python interpreter ignores shebang as this is just python comment line syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this answer does not apply on my side: stackoverflow.com/questions/78080274/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.