简体   繁体   中英

How to run .py executable under python 3 by default?

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?

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.

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

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