14

Brand new Python, just getting things set up and installed before I start messing around with things. My understanding is that there are some notable differences/incompatibilities between Python 2.7 and Python 3.3, though both versions are well used, so I thought it best to install both (In their own install directories).

When installing, I used the new 3.3 feature where the installer set the PATH variables for me, however this option is not present for 2.7. After installing both versions, I tried a quick test, opened command prompt, and typed python to bring up an interactive session, and as I hoped, it brought up python 3.3.

I am also aware of the shorter py command. However when I try py in command prompt, it brings up python 2.7. I can use py -3 to bring up 3.3, but that makes it longer than it needs to be, and seeing as I will be dealing primarily with version 3.3, I would like py and python to both bring up a Python 3.3 interactive session.

I am somewhat familiar with Window's PATH system, but I can't see why this is happening. How can I set it up so that both py and python, when typed into a windows command prompt, will start a Python 3.3 interactive session unless otherwise specified, e.g. via something like py -2?

Thanks.

2
  • 1
    Trivia: By default—if it is not overwritten with the environment variable, the ini file or with the parameter—the Python launcher will always prefer Python 2 over Python 3 installations (if both are available). This is the case because in the source, the check for version 2 simply comes first. Commented Jan 21, 2014 at 12:29
  • For me it is the opposite. py opens up python 3.8.2 and python opens up python 2.7.6 Commented Jul 1, 2020 at 5:19

2 Answers 2

34

py is the Windows Python launcher, and it can start any Python version.

On most systems py is configured to launch Python 2.7 by default if present (this is the default except for Python 3.6 and newer, where Python 3 will be run instead). You have two options if you want to change that:

  1. Set an environment variable; PY_PYTHON=3 will make py run the latest Python 3 interpreter instead.

  2. Create a file py.ini in your application directory with the contents:

    [defaults]
    python=3
    

    This has the same effect as the PY_PYTHON environment variable. Typically, your application directory is found in C:\Documents and Settings\[username]\Application Data or C:\Users\[username]\AppData\Local\py.ini, depending on the Windows version.

You can also add a #! shebang line to your scripts (first line) to tell the launcher to use Python 3 when you doubleclick such a file:

#! python3

py can also be configured to use specific Python versions when you have multiple Python 3 interpreters installed.

Sign up to request clarification or add additional context in comments.

6 Comments

That did the trick, thanks. I used the first option, as it seemed 'cleaner'.
the first line #! python3 does not work with me, why?
@zhangxaochen this works for me on Win7: #! /usr/bin/python3
What if I want to use python instead of py my vscode extensions keep trying to use python and it returns not found
|
6

"py" is the python launcher, you could choose which version to launch by adding params -2 or -3, see the help notes below:

C:\> py -h
Python Launcher for Windows Version 3.3.2150.1013

usage: py [ launcher-arguments ] script [ script-arguments ]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version

e.g., on my machine, py starts python 2 by default, adding -3 makes it start python 3:

C:\> py
Python 2.7.4 |Anaconda 1.5.0 (32-bit)| (default, Apr  9 2013, 12:19:24) [MSC v.1
500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\> py -3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

2 Comments

I understand how I can chose the version with arguments, but currently py with no arguments defaults to version 2.7, just like yours, whereas I would like it to default to version 3.3, as I will use 3.3 the most, and py is ever so slightly shorter than py -3. Is this possible?
@user2649607, I'd like make it a batch file or shortcut link, and then pin it to the task bar or start menu. Moreover, I'd like to bind the launch of the program to some hotkey using autohotkey, so that I could launch it just by pressing ctrl+shift+p (or whatever you like)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.