1

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.

jww
  • 83,594
  • 69
  • 338
  • 732
DanZoe
  • 91
  • 3
  • 13
  • You want to use one interpreter for one module and another for another module loaded in the same script? – tglaria Dec 15 '15 at 13:08
  • *This is because of the lack of modules that exist for 2.7 but not for 3.5.* This is not true at all. There is a **lot** of legacy code out there that still needs to be supported, some shops are *still* reluctant to switch for whatever reason, and there are many tutorials and books that tell students to use 2.7 for whatever reason. Python 3.0 was released in 2008, the old "lack of modules" argument just doesn't hold water these days. – MattDMo Dec 15 '15 at 13:20
  • there IS a lack of modules for python 3.5. There are a lot more SUPPORTED and WORKING modules for Python 2.7 than 3.5. I do not know what you are trying to accomplish by contradicting yourself. – DanZoe Dec 15 '15 at 17:34
  • @tglaria I have made some applications using python2.7 and some using 3.5. Basically I want the ones I've made in python2.7 to be opened with the python2.7 interpreter and the ones I've made in python3.5 to be opened with the 3.5 interpreter. – DanZoe Dec 15 '15 at 17:52

2 Answers2

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.

Community
  • 1
  • 1
nekomatic
  • 5,264
  • 1
  • 19
  • 24
  • This worked. Downloaded Python launcher from https://bitbucket.org/vinay.sajip/pylauncher/downloads I then just used regular Shebang. You can for instance do `#!python2` `#python3` or if you plan to use on unix systems: `#!/usr/bin/env python3` or `#!/usr/bin/env python2` – DanZoe Dec 16 '15 at 00:07
0

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.

Radek
  • 1,185
  • 9
  • 17
  • Hi, @radek I made a virtualenv (and the venv is using python2.7). I moved the script inside the environment, but it still uses python 3.5 when I double-click(open) the python file. (I also activated it). – DanZoe Dec 15 '15 at 17:36
  • Indeed, it does not work for double click. It is useful for the invocation from the command line. The advantage of venv is that you use just those additional libraries you need. – Radek Dec 16 '15 at 13:39