r/learnpython • • Jun 12 '24

Python works, "py" doesn't

I'm running a windows 10 machine with Python 3.12 installed. For some reason the 'python' command works fine, but 'py' gets me "'py' is not recognized as an internal or external command, operable program or batch file."

I have python installed here: C:\Users[username]\AppData\Local\Programs\Python\Python312

And the python launcher installed here: C:\Windows\py.exe

I've added both locations to my PATH, restarted the machine, but still doesn't work.

Any help is appreciated.

0 Upvotes

35 comments sorted by

4

u/Ok_Concert5918 Jun 12 '24

Then just use Python. It’s how it works for everything else. Or write an alias in the terminal.

3

u/Fred776 Jun 12 '24

py is the recommended approach on Windows these days and it should work. It's particularly useful if you need to have multiple Python versions installed.

It's just an entry point though. Once you have set up a venv corresponding to a particular python version, you would work in that and use python.

2

u/Ok_Concert5918 Jun 12 '24

I use pyenv so the shims take care of that.

-1

u/cloudiimofo Jun 12 '24

Aliases in windows get lost without setting them at the system level, so I didn't want to bother with that. I need to use 'py' specifically for existing applications that use the command.

1

u/This-Gene1183 Jun 12 '24

Env var instead?

10

u/shinitakunai Jun 12 '24

I gave up on using the path python for years, not because I couldn't but because of unexpected issues like having too many libraries installed for many projects all together in dependency hell. Nowadays I just call the specific interpreter of my project venv, it is foolproof and removes all the headache. If I am lazy I just throw a .bat shortcut to the desktop and call it a day.

1

u/notislant Jun 13 '24

Yeah used to use pycharm for it. Ended up using other languages and eventually moved to vscode. Had a few issues with venv not auto activating, but its such a life saver. Especially when projects need specific versions.

-3

u/iamevpo Jun 12 '24

Venv is same interpreter I think, but bifferent stack of libraries.

3

u/shinitakunai Jun 12 '24

No, when you create a venv the python.exe interpreter gets copied inside the venv folder as well, so you can just call that one

1

u/Bobbias Jun 12 '24

You can tell it to use symlinks rather than copy things. It's very much not recommended on windows, but it can be enabled.

On mac and linux I believe it defaults to symlinking rather than copying.

-1

u/iamevpo Jun 12 '24

It's the same. Interpreter as your base installation, right? You do not really choose a python version number with venv, I think that's not an option.

4

u/cloudiimofo Jun 12 '24

You absolutely can set the version number to any you have installed. I have legacy apps using python 2 inside of the virtual environment.

2

u/iamevpo Jun 12 '24

My point - you can inherit from any python interptrter already installed, but you cannot create an env with arbitrary python interpreter version that is not on your system - at least not with venv tool.

1

u/iamevpo Jun 12 '24

Yeah, but how do you create them?

1

u/xiongchiamiov xiongchiamiov Jun 13 '24

You don't really "set the version number". If you have multiple versions of python installed, you can install virtualenv in each one. And when you call it, virtualenv uses the version of Python it is associated with.

Compare this to pyenv, which will install arbitrary versions of python for you.

1

u/shinitakunai Jun 12 '24

What? I have many venv installed for projects, some on 3.6, some on 3.8, some on 3.11. You should give pycharm a try, it makes this extremely easy to grasp

2

u/iamevpo Jun 12 '24

venv as a tool used base interpreter version: A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python. (from venv docs).

1

u/shinitakunai Jun 12 '24

Give it a try. You install python, you create a venv, you uninstall python. The venv still should work🙃

It uses the installation of python as a base to clone itself and generate that venv interpreter. But you can have many different venvs and interpreters

1

u/iamevpo Jun 12 '24

Venv would not work like rustup for making enviroments with arbitrary python versions, just that.

2

u/Bobbias Jun 12 '24

Which is not what shinitakunai said. He explained the process:

  1. Install required version of python

  2. Generate virtual environment with desired version

  3. Uninstall the version installed in step 1.

It's helpful to actually read a post before replying to it.

His post before that one, where he suggests Pycharm leaves these details out, but after your first response he clarified and explicitly outlined those steps.

11

u/shiftybyte Jun 12 '24

Show the results of running these commands.

echo %PATH%
where py
dir c:\windows\py.exe

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jun 12 '24

Well, that's interesting. Can't say I've ever run into an issue like this before.

  • How did you install Python?

  • Have you tried uninstalling and reinstalling Python?

  • What terminal are you using? (CMD, PowerShell, Windows Terminal, Git Bash, ...)

1

u/cloudiimofo Jun 12 '24

I actually got it fixed but I'm going to answer this anyway for someone else in the future.

  • Installed using the 64-bit installer for windows

  • I uninstalled, reinstalled, restarted about a million times

  • was not working from CMD, Powershell, or GitBash

Even though I ran the original installer as admin, running the 'Repair' option in the installer as admin was the only thing that fixed it.

2

u/Bobbias Jun 12 '24

Just to be clear, in case you weren't aware (and for anyone in the future who may stumble onto this):

Recent versions of python explicitly do not add the Python folder to PATH, and opt to install py.exe in C:\windows because unless you've seriously broken your PATH, C:\windows (or wherever your windows folder is) will be on there. This sidesteps needing to add it to PATH and allows you to safely install and launch multiple Python versions without causing weird conflicts by having multiple versions in PATH.

3

u/Fred776 Jun 12 '24

I got the impression OP was aware of this but that py was not working for them for some reason.

It's certainly worth telling people about it though. I find it's not as known about as maybe it should be.

1

u/Bobbias Jun 12 '24

That's precisely why I replied anyway, because they did explicitly mention adding the Python folder to PATH, which is something that recent versions are trying to avoid doing in the first place, and because many people aren't aware of these changes.

1

u/cloudiimofo Jun 12 '24

I even selected the option to add it to PATH on install but that didn't seem to actually do anything. Had to do it manually after and then use the repair utility.

3

u/Jayoval Jun 12 '24

That's weird. I couldn't deal with that, my fingers type 'pyhton' every goddamn time.

1

u/[deleted] Jun 12 '24

Try uninstalling completely, and then reinstalling using winget?

0

u/[deleted] Jun 12 '24

[deleted]

-3

u/cloudiimofo Jun 12 '24

Whats really scary is that I've already been a python developer for years, just got a new machine and ran into this right away.

0

u/[deleted] Jun 12 '24

[deleted]

3

u/cloudiimofo Jun 12 '24

You couldn't be more right, had that talk an hour ago. His machine is next!

0

u/[deleted] Jun 12 '24

[deleted]

1

u/cloudiimofo Jun 13 '24

It's literally the standard for windows since version 3.6. https://docs.python.org/3/using/windows.html Section 4.8.