Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

windows - Python 2 and Python 3 - Running in Command Prompt

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...