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.py
script that can be run directly by test.py
inputting 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. python
In 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:
- Right-click the Computer or My Computer icon and select Properties.
- In the navigation bar, select "Advanced system settings"
- In the pop-up "System Properties" window, select the "Advanced" tab, and then click the "Environment Variables" button.
- 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.
![image_description](https://img-blog.csdnimg.cn/01e943efdc344d87a14607dbca4633ae.png)
![image_description](https://img-blog.csdnimg.cn/c81968ccfeb84d38bc5f5437fe21b9ec.png)
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](https://img-blog.csdnimg.cn/c49ac137be0649e692ea303a263d00f0.png)
- 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 list
query the directory paths of Python interpreters in all environments by
2. Add the Python suffix to the environment variable PATHEXT
Double-click the environment variable PATHEXT
to add the Python suffix .PY
and other possible suffixes .PYC
, PYO
, PYZ
, PYW
, PYZW
to it:
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.exe
file) you want
![image_description](https://img-blog.csdnimg.cn/061df053c3e8431ca16cce1a9078fec4.png)
![image_description](https://img-blog.csdnimg.cn/a0f9db8c86c84d5c8de957b7c72bf8d6.png)
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"
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.py
script 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.