How do I alias python3 on Windows?
Solution 1
I want to alias
python3
topython
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):
-
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"
-
Copy the
python.exe
file in your preferred installation of Python 3.x and rename the copied executablepython3.exe
. -
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
- Create a Windows batch file called
python3.bat
with the following information:
ex. python3.bat
C:\path\to\Python3\python.exe %*
- You can also alias Python Launcher for Windows (
py.exe
) calls in a similar manner inpython3.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 apython3
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.
HerpDerpington
Updated on September 18, 2022Comments
-
HerpDerpington over 1 year
I want to alias
python3
topython
on windows. I have already removed the alias for the microsoft store as described in this answer. Howeverpython3
is still not a known alias. How to I fix this? -
Redoman about 2 yearsFor me this doesn't work unless I type
python3.ps1
, which defeats the whole purpose of it... -
Admin about 2 yearsMaybe you didn't add the script to the environment variable? Would you mind commenting on the output of the terminal when typing
python3
? -
Admin almost 2 yearsI 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 almost 2 yearsYes, 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.