0

I am a newbie in Python and had a problem with installing Python packages on my windows computer. I installed Python itself and then had to install the packages "Numpy" and "Matplotlib" as well. My teacher told us to do it with the commands:

python -m pip install --upgrade pip
pip install numpy
pip install matplotlib

I managed to install Python but when I run the first command to install pip I got the error message telling me that Python is not found. I found the command py -m pip --version here and it worked. After running it pip got installed and I got the message "pip 22.3 from C:\Users\Kaja\Programs\Python\Python310\lib\site-packages\pip (python 3.10)". I guessed this message means that its working.

I had a similar problem with the commands to install Numpy and Matplotlib. It only worked with this commands

py -m pip install numpy
py -m pip install matplotlib

I searched the internet and found this question telling me to run the command doskey py=python and it did not work. But when I swapped py and python so that the command was doskey python=py I eventually could run my teachers commands.

I checked the windows documentation and they are using the same commands as my teacher. I am very curious to know why I had this problem because on my fellow students computers it worked without the doskey command.

Edit:

Thank you very much @RustyPython. I checked my environment variables and the path to the python.exe was already there. Out of curiousity I deleted the python.exe from the path variable and although I did that the commands still worked! Another thing I tried was to remove the doskey macro like discribed in this article but still the commands still run without any errors. I think its something else.

Kaja S.
  • 37
  • 4
  • It seems that the directory containing "python.exe" is not listed in the environment variable "PATH" (or the respective location in the registry). I can't explain why the location of "py.exe" on the other hand seems to be listed in "PATH". – Michael Butscher Oct 18 '22 at 18:49
  • 1
    @MichaelButscher, `py`'s job is to dispatch to Python interpreters, so one can have several different versions of Python installed on a system and let `py` look up the location of the one that's requested at any particular invocation time. It's entirely appropriate, given that that task, that _it_ (and not one of those individual interpreters) be in the global PATH. – Charles Duffy Oct 18 '22 at 21:46
  • 1
    @MichaelButscher, remember, Windows doesn't have the shebang mechanism UNIXy systems do -- you can't have `#!/usr/bin/env python35` vs `#!/usr/bin/env python39` to let scripts choose their own interpreters; a `.py` extension can only be associated with one thing. The goal of py.exe is to be that one universal thing that's then responsible for starting Python 3.5 for scripts written for that version, vs Python 3.9 for scripts written for _that_ version, etc. See https://peps.python.org/pep-0397/, the proposal that led to `py.exe`'s initial creation. – Charles Duffy Oct 18 '22 at 21:48

1 Answers1

1

When you install python on windows it usually comes with the python launcher. This provides a way to easily access all the python versions installed on your computer. The python launcher uses the py command which access the default python version.

py -0p will show you all the installed versions. See https://www.infoworld.com/article/3617292/how-to-use-pythons-py-launcher-for-windows.html for more details.

For the python command to work then the relevant directory containing python.exe needs to be added to your user PATH variable.

As has been said looks the the python directory is not on your PATH, but the python launcher is and finds the installed python.exe. If you want python to work on command line then you'll need to add it to your PATH variable. https://realpython.com/add-python-to-path/#how-to-add-python-to-path-on-windows

RustyPython
  • 370
  • 1
  • 7