Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Why can't I run freshly-installed Python from the command line on Windows?

+0
−0

I tried installing Python on an old computer running Windows 8[1]. I know for a fact that there was never any previous installation of Python on the machine. The installation appeared to be successful, but I can't run Python from the command line - I get the standard error 'python' is not recognized as an internal or external command, operable program or batch file..

On Windows 10, I get a stranger result: python by itself opens the Windows Store, while more complex commands report:

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

or

Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640

What's going on? I already installed Python from the official python.org distribution. I don't want to install it again from the Microsoft Store. I just want to run python. The go.microsoft.com link doesn't seem to be valid any more, either.


  1. This is a contrivance to simplify the question. At the time of writing, current versions of Python (up to and including 3.12) are supported on Windows 8.1 and newer, but Windows 7 would only support up to Python 3.8 which is about to become unsupported. ↩︎

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

Why the installed Python isn't found

By default, Windows installers for Python install Python in a folder that is not listed in the PATH environment variable. This means that executables in that folder won't be found by name; the command line needs an explicit path to them.

Rationale

Generally, programs installed "for all users" on Windows go into separate folders, side by side, within C:\Program Files (or a similarly named directory). Each program is bundled together with its associated libraries, documentation etc. in its own folder. This is different from the default Linux organization, where the main programs go directly side-by-side in /usr/bin (or similar) and the supporting files might go into /usr/lib, /usr/share (for documentation), etc. Because of this, installers for Windows programs need to add that sub-folder to PATH to make the program discoverable - entries on PATH are only checked directly, not searched.

Since the introduction of the Python Launcher for Windows in 2011, the default choice in the Windows installer is not to add the new Python to PATH. This is partly to avoid polluting the PATH (or even corrupting it: there's a length limit, and lots of other programs want to add themselves too), and partly to avoid confusion when multiple versions are installed.

After all, the entire point of the launcher is that it uses its own logic to find a Python installation (and it can even emulate how Linux treats shebang lines). So, since every installer ensures that the launcher is available, there's no need to add to PATH - unless you want python to work as a command instead of py.

Fixing the problem

It's recommended to use the Python Launcher for Windows, even with a single installation, for consistent results.

It's also possible to "repair" the installation and choose to add Python to the PATH by checking that box in the installer interface. Please carefully read every option in every installer you're given, as a general good habit; they aren't just there to annoy you.

You can also add the appropriate path to PATH explicitly from the command line or from the Windows GUI. Make sure you understand how to make these changes permanent.

Finally, you can always just use a virtual environment. They fix everything, really.

About the Microsoft Store[1]

The fact that you may get an offer to install Python when you already have it, results from an amusing combination of events.

In May 2019, a Windows 10 update added some Microsoft Store "helpers" for Python. The idea was to make it easier for Windows users who wanted to learn Python, to obtain it and start using it right away - without having to do research to find the Python web site, for example. So if a new student tried to run python, Windows would provide a helpful message and a direct way to install Python - without needing to pre-install it as part of Windows.

Unfortunately, there are some serious drawbacks to installing Python this way. It bypasses the "lengthy" setup wizard... which contains some options that some users find very useful. The update apparently also caused other issues for some users who already had Python installed when the update was released. Finally, this distribution of Python is a bit non-standard - in particular, because it's a Store app, it has limitations on file system access. Hilariously, at first, py didn't know how to launch the Store version of Python, either.

In order to make sure that these helpers are found, Windows puts their containing folder on the PATH. To try to make sure that the helpers are only found when Python is not installed, Windows puts said folder at the end of the PATH.

Which would work brilliantly, if it weren't for the fact that official Python installers had, for the last 8 years or so, been defaulting to not adding the newly installed Python to the PATH.

Oops.

(You'd think someone at Microsoft would have been aware of the potential issue. Maybe instead of a special shortcut link, they could have made an actual script that checks whether the launcher is installed. Considering that it would always be named %WINDOWS%\py.exe, that ought to be pretty easy. I'm rather disappointed at the failure to coordinate, honestly.)

Anyway, you can disable the Microsoft Store shortcuts by disabling the corresponding "App Execution Aliases" from the Windows GUI. It's a good idea to do this even if you already have python working at the command line, just to reduce the risk of future annoyance.


  1. This section is adapted from my original Stack Overflow answer. ↩︎

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »