Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...