Shebang/choose what version of Python a script is going to run

丶灬走出姿态 提交于 2019-12-02 10:12:52

问题


As a lot of people I have both the 2.7 and 3.5 version of python. Some code is backwards-compatible, some are not. This is because of the lack of modules that exist for 2.7 but not for 3.5. I therefore have a fair bit of scripts that are 2.7 and a fair bit that are 3.5. The default of .py extensions is the 3.5 version of python in my computer. My question is as follows: How can you "tell" the script to use another version of python(2.7 for instance) rather than the default I am using(3.5). I heard about Shebang, but it is Linux only.


回答1:


As mentioned in J. F. Sebastian's comment on this answer, the Python Launcher for Windows can select the Python version to run from a shebang line. According to the docs it's installed by default with Python 3.3 and later.




回答2:


The windows shell does not use the shebang line to differentiate the file type. I am assuming you want to start your py file with different interpreter than default one.

You have 2 options:

  1. If you do not mix your version 2.7 py files and 3.5 py files in one folder, you can use virtualenv http://docs.python-guide.org/en/latest/dev/virtualenvs/

    That creates the isolated environment and is the very useful approach to isolation of any project.

  2. If you have those files mixed, you just create .bat file which sets the path to your Python 2.7 interpreter and executes the py file.



来源:https://stackoverflow.com/questions/34289982/shebang-choose-what-version-of-python-a-script-is-going-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!