无法使用 Windows 提示符运行 python 脚本文件 [英] cannot run python script file using windows prompt

查看:75
本文介绍了无法使用 Windows 提示符运行 python 脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Windows 命令提示符运行 python 脚本,但收到以下错误消息:

"python: 无法打开文件 'pacman.py': [Errno 2] 没有那个文件或目录"

当我尝试命令时:

c:\Program Files (x86)\Python27>python pacman.py

这个特定的 Python 脚本文件 pacman.py 位于以下文件夹中:

C:\Users\Chris\Dropbox\edX\CS188x\search

所以我将此文件夹添加到 PYTHONPATH 并使用以下代码确认该文件夹存在:

<预><代码>>>>导入系统>>>系统路径['', 'C:\\Program Files (x86)\\Python27\\Lib\\idlelib', 'C:\\Users\\Chris\\Dropbox\\edX\\CS188x\\search', 'C:\\windows\\系统...

我还检查了这个文件的权限:

<预><代码>>>>os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.W_OK)真的>>>os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.R_OK)真的>>>os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.X_OK)真的

所以我真的不知道为什么我不能运行这个文件,即使它的路径已经添加到 PYTHONPATH 中.任何帮助将不胜感激.谢谢你.

解决方案

Python 解释器使用 PYTHONPATH.它与 Windows 的 PATH 环境变量不同.您不能将其用作在命令行上将文件传递给解释器的搜索路径.

因此,您需要指定文件的有效路径.要么使用他一直在尝试的相同命令,区别在于当前目录与 pacman.py 的位置相同,要么指定文件的完整路径.

I am trying to run a python script from the windows command prompt, but I receive the following error message:

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

when I try the command:

c:\Program Files (x86)\Python27>python pacman.py

This particular python script file pacman.py is located in the following folder:

C:\Users\Chris\Dropbox\edX\CS188x\search

So I added this folder to PYTHONPATH and confirmed that is was there using the following code:

>>> import sys
>>> sys.path
['', 'C:\\Program Files (x86)\\Python27\\Lib\\idlelib', 'C:\\Users\\Chris\\Dropbox\\edX\\CS188x\\search', 'C:\\windows\\syste...

I also checked the permissions on this file:

>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.W_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.R_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.X_OK)
True

So I am really not sure why I can't run this file, even though its path has been added to PYTHONPATH. Any help would be greatly appreciated. Thank you.

解决方案

PYTHONPATH is used by the python interpreter. It is not the same as Windows' PATH environment variable. You can't use it as a search path for passing files to the interpreter on the command line.

So, you need to specify a valid path to the file. Either by using he same command as you've been trying with the difference being your current directory is the same as the location of pacman.py, or by specifying the full path to the file.

这篇关于无法使用 Windows 提示符运行 python 脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆