2

I just installed python 3.5.1 on a win7 OS which also has python 2.7. Typing "python" into the Powershell now launches 3.5.1. Previously that launched 2.7.

How do I launch python 2.7 now? Typing "python27" or "Python27", "python2.7", "Python2.7" doesn't do it. I do have "C:\Python;" in my user Environment variable.

After reading this discussion: Python 2 and 3 from Powershell and the referenced article: http://windowsitpro.com/powershell/take-charge-environment-variables-powershell

Am I correct that Powershell can only do one or the other, i.e. if I want to use 2.7 in Powerhell I have to activate and deactivate it?

Is there is a way to simply type "python27" to launch 2.7?

If it is helpful, this is my user env PATH:

C:\Users\patrick\AppData\Local\Programs\Python\Python35-32\Scripts\;
C:\Users\patrick\AppData\Local\Programs\Python\Python35-32\;
C:\ProgramData\Oracle\Java\javapath;
C:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files (x86)\Windows Live\Shared;
C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;
C:\Program Files (x86)\Common Files\Roxio Shared\12.0\DLLShared\;
C:\Program Files\WIDCOMM\Bluetooth Software\;
C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;
C:\Program Files (x86)\QuickTime\QTSystem\;
C:\Program Files (x86)\Skype\Phone\;
C:\Program Files\MATLAB\R2015b\bin;
C:\ffmpeg\bin;
C:\Python27;
C:\SQLite3
Community
  • 1
  • 1
MmmHmm
  • 3,435
  • 2
  • 27
  • 49
  • 1
    python.exe from the 32-bit Python 3.5 installation is found in the second `PATH` entry, while python.exe from Python 2.7 is found in the second to last entry. You can use `py -2` to run Python 2.7, or manually modify your system and user `PATH` values to prioritize 2.7 over 3.5. Note that your per-user value gets concatenated to the end of the system value when you log on. – Eryk Sun Jun 04 '16 at 03:36
  • 1
    Also, you have a redundant and broken entry for PowerShell (the 3rd entry), which you can remove. – Eryk Sun Jun 04 '16 at 03:38
  • You can try navigating to the directory where 2.7 is installed and then executing it through cmd. – shiva Jun 04 '16 at 04:02
  • @eryksun thank you for the edit - I am used to Ubuntu where I can just type python2 or python3 in to the terminal. The powershell and windows OS are not at all my strong suit... py -2 and py -3 work as expected. Will remove the PShell redundancy (and take some time to better grok setENV) – MmmHmm Jun 04 '16 at 04:06
  • @shiva that sounds like work ;) – MmmHmm Jun 04 '16 at 04:09
  • 1
    The core Windows developers have always been against using python2.exe, python2.7.exe, and so on, so that isn't going to happen. But you can make these copies for yourself if you prefer that to using py.exe. But leave scripts associated with py.exe because it handles shebangs (including virtual ones such as `#!/usr/bin/python3`). – Eryk Sun Jun 04 '16 at 04:42
  • @eryksun py -2 & py -3 will get the job done. Oddly, the to get the py3 IDLE up and running I had to cd into C:\Users\patrick\AppData\Local\Programs\Python\Python35-32\Lib\idlelib and invoke it in the powershell as .\idle (idle and idle3, no go). I assume it is the way I installed it, but it sure would be nice to have py3 at the top level like py2 (C:\Python27\). Oh well. – MmmHmm Jun 04 '16 at 06:06
  • 1
    PowerShell doesn't search for executables in the current directory. Neither does cmd if you set the environment variable `NoDefaultCurrentDirectoryInExePath`. That's why it's `.\idle` instead of `idle`. But really there's no need to use the batch file. You can use `pyw -3 -m idlelib`. For Python 2 use `pyw -2 -m idlelib.idle`; you have to run the idle.py script since Python 2 doesn't grok `__main__.py` scripts. – Eryk Sun Jun 04 '16 at 07:22

2 Answers2

3

Python installations place a a stub executable named py.exe in $env:SYSTEMROOT (typically: C:\Windows), to which you can pass an option to specify the version you want to run: -2 to launch an installed 2.x version, and -3 for an installed 3.x version (seemingly, the default is the 2.x version).

Thus, given that $env:SYSTEMROOT is normally in the path, use the following to run a Python 2.x script from PowerShell:

py.exe -2 ...
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

For the sake of simplicity, I would suggest you use WinPython distribution. There is a winpython command prompt bundled inside. You can open the It in the specific Python version and that Python will be invoked!

MaThMaX
  • 1,995
  • 1
  • 12
  • 23