11

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.

EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".

Alex
  • 3,946
  • 11
  • 38
  • 66

4 Answers4

24

the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".

To run Python 2 executable:

C:\> py -2

To run Python 3 executable:

C:\> py -3

where py is a Python launcher that is bundled with your Python 3 installation.

py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:

C:\> py your_script.py

and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.

is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?

OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.

There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.

Is there some kind of way to configure Python so that it runs like this?

If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:

"C:\path to\Python 3.X\python.exe" %*
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

You likely missed the checkbox at the bottom of the installer.

Full documentation here: https://docs.python.org/3/using/windows.html

Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.

Install window

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    I very much clicked the box. However, to run from the command line, you type "python" instead of "python3". – Alex Dec 11 '15 at 05:08
  • That's a problem because I have both python2 and 3 installed, so it is ambiguous (you use "python" to run both) – Alex Dec 11 '15 at 05:08
  • Windows looks at the path and takes whichever "python" comes first. – Alex Dec 11 '15 at 05:09
  • 1
    I'm well aware of the PATH workings. You should've specified you had python2 already. I think you are able to rename the python.exe from Py3 to python3.exe – OneCricketeer Dec 11 '15 at 05:13
1

You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.

  • I very much clicked the box to add it to path. However, to run from the command line, you type "python" instead of "python3". – Alex Dec 11 '15 at 05:08
  • who upvotes it? Look at your Python installation: there is no bin folder on Windows. There are `python.exe`, `pythonw.exe` files in the root of Python installation. There is no `python3.exe`. – jfs Dec 11 '15 at 12:33
0

I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:

> SelectPython C:\Path\To\Desired\Version\Of\Python
> python

or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:

> SelectPython C:\Path\To\Desired\Version\Of\Python  python -c "import sys;print(sys.version)"

You may find it helpful.

jez
  • 14,867
  • 5
  • 37
  • 64