Skip to main content
 首页 » 编程设计

python之使用 .py 文件作为命令行参数调用 python 解释器不起作用

2025年05月04日21xing901022

我已将我的路径环境变量设置为包括 python 解释器以及我的“python_scripts”文件夹。我可以从我机器上的任何地方单独调用 python 解释器或位于我的“python_scripts”文件夹中的任何 .py 文件,如下所示:

C:\> python.exe 
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64  
bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 

或

C:\> test_code.py 

而且它们都工作正常。但是,当我调用 python 解释器后跟脚本名称时:

C:\> python.exe test_code.py 

它返回以下错误,除非我位于我的“python_scripts”文件夹中:

python: can't open file 'test_code.py': [Errno 2] No such file or directory 

为什么会这样?

请您参考如下方法:

如果您使用 C:\> python.exe test_code.py,它将在您的当前目录中查找 test_code.py。如果您将参数传递给 python.exe,它必须是存在的文件的有效绝对路径或相对路径。