Home  >  Article  >  Backend Development  >  How to enable python after installation

How to enable python after installation

尚
Original
2019-06-25 10:17:589072browse

How to enable python after installation

Python Launcher for Windows is a utility that helps locate and execute different Python versions. It allows a script (or command line) to indicate preferences for a specific Python version and will locate and execute that version.

Unlike the PATH variable, the launcher will correctly select the most appropriate Python version. It prefers to install by user rather than system, and sorts by language version rather than using the most recently installed version.

Launchers were originally specified in PEP 397.

Several ways to start Python:

1. Start Python from the command line

Installing Python 3.3 and above globally will put the launcher on your PATH. The launcher is compatible with all available Python versions, so it doesn't matter which version is installed. To check if the launcher is available, execute the following command in the command prompt:

py

will find that the latest version of Python installed is started - it can exit normally, and any other command line arguments specified will be Sent directly to Python.

If you have multiple versions of Python installed (for example, 2.7 and 3.7), you will notice that Python 3.7 starts - If you want to start Python 2.7, try the command:

py -2.7

If you want To use the latest version of Python 2.x, try the following command:

py -2

You will find that the latest version of Python 2.x has started.

If you see the following error, you do not have the launcher installed:

'py' is not recognized as an internal or external command,
operable program or batch file.

Single-user installations of Python will not add the launcher to the PATH unless that option is selected during installation .

2. Start Python from a script

Let’s create a test Python script - create a file called ``hello.py`` with the following content

#! python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))

From the directory where hello.py is located, execute the following command:

py hello.py

You should notice that the version number of the latest Python 2.x installation is printed. Now try changing the first line to:

#! python3

Now, re-executing the command should print the latest Python 3.x information. As with the command line example above, you can specify the version qualifier more explicitly. Assuming you have Python 2.6 installed, try changing the first line to #! python2.6 and you will find the 2.6 version information printed.

Please note that unlike interactive use, bare "python" will use the latest version of Python 2.x you have installed. This is for backward compatibility and Unix compatibility, where the command python usually refers to Python 2.

3. Start Python from a file association

When installing, you should associate the launcher with the Python file (i.e. .py, .pyw, .pyc file) associated. This means that when you double-click one of the files from Windows Explorer, the launcher will be used, so you can use the same tool above to let the script specify which version it should use.

The main benefit of this is that a single launcher can support multiple Python versions at the same time, depending on the content of the first line.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to enable python after installation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn