Keywords: Python | Windows 10 | App Execution Aliases | CMD | Microsoft Store
Abstract: This article provides an in-depth analysis of why the 'python' command in CMD opens the Microsoft Store instead of executing Python in Windows 10, focusing on the App Execution Aliases mechanism. It offers step-by-step solutions to disable aliases and use alternatives like the 'py' launcher, covering Path environment variable settings and best practices to ensure a smooth Python development environment.
Problem Description
Many Windows 10 users encounter an issue where typing the 'python' command in Command Prompt (CMD) opens the Microsoft Store to install Python, rather than executing the installed Python interpreter. This problem often occurs after installing Python from the official Python website, even when the Path environment variable is correctly configured. For instance, when attempting to run simple code in Sublime Text 3, users may receive a message such as "Python was not found but can be installed from the Microsoft Store", disrupting the development workflow.
Cause Analysis
The root cause of this issue lies in the App Execution Aliases feature introduced in Windows 10. During system initialization or updates, Microsoft creates execution aliases for Python, which are virtual executable files located in the %USERPROFILE%\AppData\Local\Microsoft\WindowsApps directory. When a user types 'python' in CMD, the system searches for executable files in the directories listed in the Path environment variable in order. Since the WindowsApps directory is typically at the top of the Path list, CMD finds these alias files first, instead of the actual installed Python executable. These alias files are designed to redirect users to the Microsoft Store if Python is not installed, but if Python is installed from other sources, it leads to conflicts. Additionally, the alias files are not entirely empty; they may contain commands like "start ms-windows-store:" to trigger the Store opening.
Solution
To resolve this issue effectively, the recommended approach is to disable the Python aliases in the App Execution Aliases. The step-by-step procedure is as follows: first, use the Windows search bar to search for "Manage app execution aliases"; second, in the opened window, locate the aliases named "python" and "python3"; finally, uncheck the checkboxes for these aliases to disable them. After disabling, the system will delete the corresponding virtual files and prevent their regeneration, ensuring that CMD finds the actual installed Python executable when searching the Path. This method is straightforward and reliable, avoiding issues where manually deleted files regenerate.
Alternative Methods
In addition to disabling the aliases, users can employ the Python "py" launcher as an alternative. If the user selected to install the py launcher during Python installation, they can type "py" in CMD instead of "python" to start the Python interpreter. For example, entering <code>py -3.11</code> can specify running Python version 3.11. This approach not only avoids alias conflicts but also supports managing multiple Python versions, enhancing flexibility. Another method involves adjusting the order of the Path environment variable, such as moving the WindowsApps directory to the bottom or removing it, but this is not advisable because the directory contains aliases for other applications (e.g., Notepad, Xbox Game Bar), and modifications could impair system functionality.
Conclusion
By thoroughly understanding the App Execution Aliases mechanism, users can systematically resolve the issue of the 'python' command opening the Microsoft Store in CMD. Disabling the aliases is the preferred solution for its simplicity and efficiency, while using the 'py' launcher offers additional flexibility, particularly for multi-version Python environments. It is recommended that users pay attention to relevant settings during Python installation, preferably downloading and installing from the official website to ensure full development features, such as the creation of the Scripts folder. In summary, proper system configuration can significantly improve the Python development experience and prevent unnecessary disruptions.