Loading

Configure Python in Windows

by drk last updated on 2015/07/27 ( python / programming / windows )


This guide is about how to deal with multiple python versions on windows.

Python Launcher for Windows

When installing python, two executables will be added to the windows directory: C:/Windows/py.exe and C:/Windows/pyw.exe, which you can use to run any of the python versions installed.

You can run scripts like so.

py -3 file.py  # use the most recent python 3.x version
py -3          # open in interactive mode
py -2 file.py  # use the most recent python 2.x version
py file.py     # use the version specified by the shebang
               # otherwise use the default version
py -3.4 file.py  # use this specific python version
py -3 -m pip [arguments]  # can also access pip like this

The python version can be specified in the script file itself, with the addition of a line in the beginning of the script, the shebang.

#! interpreter [optional arguments]
#! python2 -- most recent python 2.x
#! python3 -- most recent python 3.x
#! python3.4 -- this specific version

Alternative way

You can add both directories to the 'PATH', and rename the executables.

After installing both versions, you'll have two directories installed, for example in C:/Python27 and C:/Python34. Now you can add them both to the 'PATH' environment variable.

To add to the 'PATH' variable.

  • Open the start menu -> search for environment variables.
  • Or start -> right click My Computer -> Properties -> Advanced system settings -> Environment Variables.
  • Find the 'PATH' variable and click 'edit'.
  • At the end, add the relevant paths, so for example: (other paths here);C:\Python34\;C:\Python27\
  • Make sure there's a ; between paths (the delimiter character).
  • You can also add other relevant python directories if needed (like the 'scripts' directory).

At this point, you can run 'python' in the command line, but it will only run one of the versions. To be able to distinguish between the different versions, you need to rename the python executables, for example renaming C:/Python27/python.exe to C:/Python27/python2.exe, and doing the same for the python3 executable, will allow you to run the desired version by calling either the python2 or the python3 command.

Extra information

There's several ways to open the command line in windows.

  • start -> search for cmd (or powershell) -- opens in your home directory.
  • In the folder explorer, right click while pressing the shift key, and you'll see an entry saying: "open command window here".
  • Or, also in the folder explorer, go to the path/address bar (alt + d) and write cmd or powershell. -- also opens in the current directory.
back

Categories