L
L
leonidpetrosian2019-04-01 21:16:38
Python
leonidpetrosian, 2019-04-01 21:16:38

How does shebang and python interpreter work?

All the best!
Please explain one very stupid, but haunting me "trick".
When creating any project for Python, it is considered good practice to create a virtual environment for it.
$ python3 -m venv env
By activating the environment, the Python interpreter "moves" to the env folder, which can be easily seen

$ which python3
users/username/project/env/bin/python3

In order not to prescribe the interpreter in the terminal each time, we prescribe the path to it in the shebang.
BUT why then
#!/env/bin/python
print('Hello World!')

but not
#!users/username/project/env/bin/python3
print('Hello World!')

PS:
And for that matter, is it possible to write in the shebang not the full path to the interpreter, but only relative to the working directory? Something like:
#! ~/env/bin/python3
print('Hello World!')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2019-04-01
@saboteur_kiev

/usr/bin/env is the command that will find the correct binary. Because not in all cases, you have python in /usr/bin, not in all cases you have bash in /usr/bin
It's just more stability for scripts that can run on embedded linux, cygwin windows and other environments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question