7

Having various projects in both Python 2 and Python 3 (with both python versions installed), I was looking for a more intuitive way to run scripts via Command Prompt than py -3 script.py.

Python 2 already took python script.py, so ideally python3 script.py should invoke Python 3.

My question: How can I add python3 as a Command Prompt command?

dimo414
  • 47,227
  • 18
  • 148
  • 244
Jack
  • 380
  • 1
  • 3
  • 12

2 Answers2

8

If Python 2 and 3 are both installed and in the PATH variable, you can do:

py -2

or

py -3

Jérémie RPK
  • 297
  • 4
  • 13
6

Searching did not yield good results, so I thought I should share the process I took with anyone looking for this in the future.

  1. Make sure the Python 3 folder is present in the PATH environment variable.
  2. Locate the "python.exe" file in the Python 3 folder.
  3. Copy and Paste the "python.exe" file within the Python 3 folder.
  4. Rename the copied file to "python3" (or whatever you want the command to be).

Now, when you input python3 script.py to Command Prompt, the script will run through the copied Python 3 file.

Also, by copying python.exe (instead of renaming it) you allow other interpreters - such as PyCharm - to continue using their default "python.exe" path settings.

I hope this helps!

EDIT:

A "symlink" has the same effect, but keeps things a bit tidier.

Jack
  • 380
  • 1
  • 3
  • 12
  • 3
    Rather than copying the `python` executable simply create a [symlink](http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/). – dimo414 Aug 03 '16 at 04:41
  • I'm fairly certain after some searching, I could find this exact solution elsewhere, but glad you solved it – OneCricketeer Aug 03 '16 at 04:45
  • 1
    ahh! This is not a good solution at all! I'm glad it worked but (a) properly configuring your `%PATH%`, and (b) using the [Python Launcher](https://docs.python.org/3/using/windows.html#python-launcher-for-windows) should be all that you need. If for some reason it's not, then dimo414's suggestion of a symlink is much better. – jedwards Aug 03 '16 at 04:49
  • 2
    The reason you want a symlink is that a "python3.exe" symlink will still be valid if "python.exe" gets replaced by an in-place upgrade. You don't have to remember to copy the file again. Just run `mklink python3.exe python.exe` in an administrator command prompt. – Eryk Sun Aug 03 '16 at 06:19
  • 1
    Ah! I had never heard of a symlinks before! Thank you for this information. – Jack Aug 03 '16 at 15:02
  • I wasted a solid 2 hours on this. I was using `subprocess.call` with 2 python versions installed (both in path), but creates all kinds of confusion as the subprocess module is so ambiguous to begin with. I just ended up creating a copy called python3 and used the direct path to that folder. Sighs. – ajsp Jul 11 '21 at 08:34