2

I don't like the other post. because it involves renaming python executables.

1
  • Please make this into a question with details (such as restrictions and specific goals). Commented Nov 24, 2017 at 16:39

3 Answers 3

1

Here's my discoveries.

Step 1. Go to System Properties. Click on Environment Variables

Step 2. Add new variables, such as PYTHON_27_HOME

  • PYTHON_27_HOME:%ProgramFiles%\Python27\
  • PYTHON_36_HOME:%ProgramFiles%\Python36\
  • PYTHON_HOME:%PYTHON_27_HOME%

In my case, PYTHON_27_HOME(Python 2.7) is pointing to C:\Program Files\Python27\. You can replace this with your own path to python. %PYTHON_HOME% has a default value pointing to %PYTHON_27_HOME%, which is the path for Python 2.7. That's my preference, feel free to adjust accordingly. Be aware that there're 32-bit and 64-bit python. Please use %PROGRAMFILES% for path to C:\Program Files and %PROGRAMFILES(X86)% for path to C:\Program Files (x86).

Step 3. Select PATH and click on Edit. PATH

Step 4. Click on New and add %PYTHON_HOME%. %PYTHON_HOME% will be added to PATH automatically every time you launch a command prompt.


In order to switch between different versions of python in cmd, here's the trick.

Step 5. I created a batch file with

@echo off
:: Switch Python version
DOSKEY python27=SET PATH=%PYTHON_27_HOME%;%PATH%
DOSKEY python36=SET PATH=%PYTHON_36_HOME%;%PATH%

Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, python27 or python36 is called, it re-exports %PATH% with the new Python path. Save it as profile.bat. You can name it whatever you want.

Step 6. Search for regedit (Registry Editor). Click on Edit > New > String Value. Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data. Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat, which creates those two alias in the script.

Step 7. Close any command prompt you're using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.

Step 8. Verify your results here.

If you're using different Java versions, same trick applies, too. Find my javac environment setting here.

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

4 Comments

Wow! That seems extremely convoluted and complicated. Doesn't Windows support version managers, like asdf or pyenv?
It's just a taste and preference. I don't refer to use third party version manager.
windows has a py frontend which can run python 2 or python 3 depending on the shebang. Even if this solution above works, don't expect anyone to follow that in an industrial scale...
Windows has a py frontend? Any references to this py frontend thing appreciated.
1

I think the easiest way to support various versions of Python, as well as other languages, is the asdf version manager. It allows you to set a version of Python globally, as well as in each project folder. This means that you can set your Python version to dynamically change based upon the folder you're working in.

asdf version manager

I haven't used Windows for almost 20 years, but I've heard that Windows 10 sports an Ubuntu-based subsystem for Linux. I don't know if asdf will work with that, but it is worth a try. Just use the instructions for setting asdf up with bash.

2 Comments

The question was specifically about Windows, which asdf doesn't support.
@looeee - First of all, I clearly addressed that fact in my post. Secondly, it does indeed work if you enable WSL. If you're the one who down-voted me, I would greatly appreciate it if you would reverse that action, since what I said is accurate and does indeed work.
1

TL;DR: Use a symbolic link to youre current version and put that on your path instead.

Linux has the alternatives command to switch versions globally, but I do this:

  1. Install Python variants under C:\Python\Python38, C:\Python\Python36 etc
  2. Set your PATH to include C:\Python\Current;C:\Python\Current\Scripts
  3. Have a batch file like this to switch versions:
    @echo off
    echo 1. Python 3.8
    echo 2. Python 3.6   
    set /p ver="Enter Version: "
    
    if [%ver%]==[1] (
        SET FOLDER=C:\Python\Python38
    ) ELSE if [%ver%]==[2] (
        SET FOLDER=C:\Python\Python36
    ) ELSE (
        GOTO end
    )
    
    if exist C:\Python\Current\nul (
        rmdir C:\Python\Current
    )
    
    MKLINK /D C:\Python\Current %FOLDER%
    
    :end

The only downside is that MKLINK (and hence the batch file) requires elevated privilages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.