简体   繁体   中英

how to specify version of python to run a script?

I'm learning python now using a mac which pre-installed python 2.7.5. But I have also installed the latest 3.4.

I know how to choose which interpreter to use in command line mode, ie python vs python 3 will bring up the respective interpreter.

But if I just write a python script with this header in it "#!/usr/bin/python" and make it executable, how can I force it to use 3.4 instead of 2.7.5?

As it stands, print sys.version says:

2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]

Set the shebang (script header) to the path to python3.4 which you can get using which .

For example, here's what do I have:

$ which python
/usr/bin/python
$ which python3.4
/usr/local/bin/python3.4

Then, just set the shebang appropriately:

#!/usr/local/bin/python3.4

您知道,您可以使用py -specific version启动 python 要在具有特定版本的解释器上运行脚本,您只需使用以下参数启动脚本, py yourscript.py -version

You can add to the Python script a first line of:

#!/usr/bin/env python3

It is better than setting the path or the exact python 3 sub version if not needed.

在python中,您可以使用:

py yourscript.py -version

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