What is the difference between 'py' and 'python' in the Windows terminal?
Asked Answered
P

3

75

Why is there a difference between 'py' and 'python', when I'm using to pip to install modules through the command:

python -m pip install [Mod]

or

py -m pip install [Mod]

The modules aren't available when I'm using the Python IDLE. Furthermore, when I'm checking the

sys.path

it's different for both 'python' and 'py'. How do I make it so they are both the same, and when installing modules, installing into the same folder where they can both access.

Edit:

I forgot to mention that this on windows. So Anyways, I executed

python -V

and it says the version is "Python 3.6.4:: Anaconda, Inc"

While:

py -V

gives "Python 3.6.5". How much difference is there? and why do they have different paths if they are the same version(3.6)?

Parley answered 17/6, 2018 at 12:9 Comment(0)
E
114

On Windows

python is the Python executable of the Python installation which you have selected as a default during the installation. This basically put the path to that version inside the PATH, so that the executable is directly available.

py is the Python launcher which is a utility that comes with Python installations on Windows. It gets installed into C:\Windows\ so it’s available without requiring PATH modifications. The Python launcher detects what Python versions are installed on your machine and is able to automatically delegate to the right version. By default, it will use the latest Python version that is on your machine. So if you have installed 2.7, 3.5 and 3.6, running py will launch 3.6. You can also specify a different version by doing e.g. py -3.5 to lauch 3.5, or py -2 to launch the latest Python 2 version on your machine.

You can read more about the launcher in the documentation.

These days, I personally never put Python directly in my PATH. I only use the launcher for everything as that gives me more control over what Python will be launched. If you see that py -m pip install will not install modules for the Python version you run with IDLE, you should check what versions there are. Every Python installation comes with its own directory where pip modules are installed in. So if you e.g. launch IDLE for Python 3.5, you need to make sure that you also run pip with Python 3.5 (e.g. py -3.5 -m pip install).

On Linux

python is a symlink to the default Python installation on your machine. For many Linux machines, this will only be Python 2. Even distributions that no longer come with Python 2 but only ship Python 3 will not use python for Python 3 since the general expectations of tools would be that python is a Python 2. So they may only have a python3 symlink.

py usually does not exist on Linux, unless you set an alias or symlink yourself. You can check with which python and which py to see what these commands actually are.


Anaconda

The Python version you are using is from Anaconda, which is a different Python distribution targeted at data scientists that comes bundled with quite a few things. It uses a different Python version that is separate from the official CPython releases that are available from python.org. I assume that those versions simply won’t be available through the Python launcher by default.

Eyeleen answered 17/6, 2018 at 12:21 Comment(1)
Maybe you know the answer for my question related to your post.Characteristic
T
3

@poke gave a great answer. I'd just like to add that there's a #!/usr/bin/env python2 comment you can add at the top of Python files to tell it what version of Python to use.

python command line command ignores the comment. py parses the comment and uses the correct version.

Personally, I'll be using py for executing files.

Template answered 26/8, 2020 at 9:46 Comment(1)
The shebang line is not ignoredEadwine
D
2

poke's answer is great, but if you want to use "python" you will either need to type the entire path, or add the installation directory to your system’s PATH variable.

This is the solution if you're on Windows:

  • Go to system
  • Go to the advanced system settings
  • Go to the advanced tab
  • Go to Environment variables
  • Once you're there go to system variables and select the one that says 'PATH' and click on edit
  • Click on New and then copy your script path, it generally is in this route C:\Users\pc\AppData\Local\Programs\Python\Python311\Scripts

This "solved" it in my case.

Detestation answered 4/2, 2023 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.