Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 600
Portable Avatar asked Jun 17 '18 12:06

Portable


People also ask

Why does Windows use py instead of Python?

Difference “python” vs “py” The command py refers to the Python launcher, a utility that's automatically installed into C:\Windows\ for any Python installation on Windows. All files in the Windows folder are accessible without needing to modify the PATH environment variable.

Why does py work in cmd but not Python?

py is itself located in C:\Windows (which is always part of the PATH ), which is why you find it. When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal.

What is py command Windows?

The py launcher—or just py for short—is a shortcut to all of the installed versions of Python on one's system. With a command-line switch, you see at a glance all of the Python interpreters you've installed, and invoke a specific version of Python, whether 32-bit or 64-bit.

What is py command in Python?

py command comes with Python3. x and allows you to choose among multiple Python interpreters. For example, if you have both Python 3.4 and 2.7 installed, py -2 will start python2. 7, and py -3 will start python3.


2 Answers

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.

like image 72
poke Avatar answered Sep 19 '22 14:09

poke


@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.

like image 26
RedDragonWebDesign Avatar answered Sep 19 '22 14:09

RedDragonWebDesign