Reputation: 6492
This question has probably been asked to death, but I can't seem to find the answer.
I can run python 3 script like this:
python3 my.py
However, suppose I make it executable and run it on its own. Then it typically executes under python 2, but I want it under python 3. Is this possible?
chmod u+x my.py
./my.py #executes under python 2.
Can I add something to the first line of the script? Or can I change global system default? (I'm on OS X). Also, is it safe to change global default, will any of my applications stop working?
Upvotes: 1
Views: 165
Reputation: 1543
I think that if you write
#!/usr/local/bin/python3
as the first line of the file, it will be executed b python 3.
Note that you must execute it as:
./my.py
if you do
python my.py
I have no idea what might happen. If your default python version is 2.7, it will probably be executed by that version.
Upvotes: 1
Reputation: 160377
Almost all *nix
variants support the following:
#!/usr/bin/env python3
So adding it as the first line will find Python 3 assuming the path where python resides is including in the $PATH
Upvotes: 1