How do I alias python3 on Windows?

17,792

Solution 1

I want to alias python3 to python on Windows.

You haven't mentioned what release of Python you are using (e.g. "vanilla" Python, Anaconda, SciPy, etc.), but if I am not misunderstanding, this likely shouldn't be necessary. For Windows versions of Python (i.e. not WSL), python3 is not typically a standard alias. That is, the Python 3.x executable is usually just python (python.exe).

Be aware that python.exe should be in a folder listed in your Windows Path if you want to access it as just python. You can find a refresher on editing Windows Path variables here.

If you meant the reverse (i.e. you want to run Python 3.x with python3), then there are at least a few possible approaches (each item below is a separate option):

  1. Make symbolic link called python3.exe to your preferred copy of Python 3.x with mklink:

    mklink "C:\path\to\symlink\python3.exe" "C:\path\to\Python3\python.exe"
    
  2. Copy the python.exe file in your preferred installation of Python 3.x and rename the copied executable python3.exe.

  3. If you aren't set on specifically using python3 and have the Python Launcher for Windows (py.exe) installed which comes with "vanilla" Python from python.org, you can use:

    # Use the "default" installation of Python 3.x, 
    # as detected by the Python Launcher for Windows.
    
    py 3
    

or:

   # Use a specific version of Python 3.x if you have more
   # than one version installed, outside of a virtual environment.

   py 3.8
  1. Create a Windows batch file called python3.bat with the following information:

ex. python3.bat

    C:\path\to\Python3\python.exe %*
  1. You can also alias Python Launcher for Windows (py.exe) calls in a similar manner in python3.bat e.g.:

ex. python3.bat

    py 3 %*

or:

ex. python3.bat

    py 3.8 %*

With all the options above, any "python3" file you wish to access from the command line (i.e. any symbolic link, a renamed executable copy, py.exe or python3.bat) must be in a folder located in your Windows Path.

Notes

  • These options obviously do not include any option to "alias" python.exe at the command line (e.g. via doskey macros, as mentioned in the comments, or possibly via actual alias commands specific to a given terminal). It also ignores the possibility that some distributions of Python could (theoretically) already come with a python3 alias.

  • Creating a symbolic link with mklink on Windows may require administrative privileges on versions of Windows earlier than some early releases of Windows 10.

  • When creating a symbolic link, be certain to include the file extension (.exe). This is not optional if you want the link to work correctly.

  • It is likely best to leave any renamed executable copy (option 2) in the same folder as the original python.exe.

Solution 2

If you are still wondering how to do it, Here is one workaround!
Create a PowerShell file named python3.ps1 and inside that just type python and save it anywhere you like.
Copy that path and add that path to the Environment path. Now you are ready to go!
Just type python3 and it runs python.
you can also use the python command to use python.

Share:
17,792
HerpDerpington
Author by

HerpDerpington

Updated on September 18, 2022

Comments

  • HerpDerpington
    HerpDerpington over 1 year

    I want to alias python3 to python on windows. I have already removed the alias for the microsoft store as described in this answer. However python3 is still not a known alias. How to I fix this?

  • Redoman
    Redoman about 2 years
    For me this doesn't work unless I type python3.ps1, which defeats the whole purpose of it...
  • Admin
    Admin about 2 years
    Maybe you didn't add the script to the environment variable? Would you mind commenting on the output of the terminal when typing python3?
  • Admin
    Admin almost 2 years
    I added the path where the script is located to the environment variable, not the script itself... it doesn't make sense to add a file to a PATH variable, does it?
  • Admin
    Admin almost 2 years
    Yes, you need to add path to the environment variable not the file itself. And it only works from powershell as far as I know. If you want to run the command from cmd, you need to add .bat file also. It may work after that.