Configure the Windows terminal to execute Python scripts directly without typing "python"

In the Linux system, specify the Python interpreter path at the beginning of the Python script, and you can directly call the Python script as a command in the terminal. For example, we have a test.pyscript that can be run directly by test.pyinputting instead ofpython test.py

When using the windows system, it is often found that the Python interpreter needs to be specified before the Python script. pythonIn fact, in the Windows system, by manually adding the path of the Python interpreter to the system environment variable, you can also implement the Python script directly without inputting in the terminal, just like the Linux system. The specific configuration content is as follows:

1. Add Python to environment variables

1.1 Open the environment variable setting window

Perform the following operations in sequence to open the environment variable setting window:

  1. Right-click the Computer or My Computer icon and select Properties.
  2. In the navigation bar, select "Advanced system settings"
  3. In the pop-up "System Properties" window, select the "Advanced" tab, and then click the "Environment Variables" button.
  4. In the Environment Variables window you can see 系统环境变量and 用户环境变量.
    • 系统环境变量is a variable that applies to all users and requires administrator privileges to modify.
    • 用户环境变量It is only applicable to the current user, and each user can modify it by himself.

tps://img-blog.csdnimg.cn/b4c44b4d430842a983ca31ac009bb670.png)

image_description image_description

1.2 Add Python to environment variables

Select in the system environment variable Path, double-click or click 编辑, click New, and then enter the directory path of the Python interpreter.

image_description
  • If the official version of Python is installed, the directory path of the Python interpreter is the installation directory of Python
  • If conda is installed, you can conda env listquery the directory paths of Python interpreters in all environments by

insert image description here

2. Add the Python suffix to the environment variable PATHEXT

Double-click the environment variable PATHEXTto add the Python suffix .PYand other possible suffixes .PYC, PYO, PYZ, PYW, PYZWto it:

insert image description here

3. Modify the default opening method of Python script

Select a Python script, right-click, select Properties, change the opening method, drag it to the bottom, click 在电脑上选择应用, and find the Python interpreter ( python.exefile) you want

image_description image_description

Then, open CMD as an administrator and enter:

assoc .py

If output:

.py=Python.File

It means that the association has been successful. If the output is empty or other values, execute the association command:

assoc .py=Python.File

Next query the Python interpreter:

ftype | find "Python"

insert image description here

If the output is empty, or the output Python is not the desired Python version, you can modify the Python interpreter

 ftype Python="your_python_path\python.exe" "%1" %*

Note: If ftype | find "Python"multiple records containing Python interpreters are output, all of them need to be modified.

4. Add the Python script command to the environment variable

If some Python scripts are invoked in the form of a command line, the folder where they are located can also be added to the environment variable Path, so that the script can be invoked from any location.

5. Test

After the above steps are completed normally, you can now directly call the Python script in the terminal to run it directly.

For example, create a test.pyscript to output the current Python version information and path:

import sys
print(sys.version)
print(sys.executable)

Call this script directly in the terminal to output Python information directly.
insert image description here

Guess you like

Origin blog.csdn.net/qq_27386899/article/details/130597088