简体   繁体   English

无法执行 Python 脚本

[英]Can't Execute Python Script

I'm learning Python via 'Automate the Boring Stuff with Python' and I'm stuck on being able to execute my programs.我正在通过“用 Python 自动化无聊的东西”来学习 Python,但我一直坚持能够执行我的程序。 I have the directory to my Python install added to my system environment variables (see below).我已将 Python 安装的目录添加到我的系统环境变量中(见下文)。 When I run 'py' or 'python' from the terminal, it does bring up the Python window.当我从终端运行“py”或“python”时,它会打开 Python 窗口。 However, when I try to run a specific program (for example, named pw.py) by typing 'py pw.py' or 'python pw.py' nothing happens.但是,当我尝试通过键入“py pw.py”或“python pw.py”来运行特定程序(例如,名为 pw.py)时,没有任何反应。 I have a shebang line in my program (#! python3).我的程序中有一个 shebang 行(#!python3)。 It looks like the window briefly tries to come up, but then nothing.看起来窗口短暂地试图出现,但什么也没有。 Any ideas?有任何想法吗?

Here is the program:这是程序:

#! python3
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6','blog':'VmALvQyKAxiVH5G8v0lif1MLZF3sdt','luggage': '12345'}
import sys, pyperclip
if len(sys.argv) <2:
    print('Usage: python pw.py[account] -  copy account password')
    sys.exit()

account = sys.argv[1]  #first command line arg is the account name

if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print ('Password for ' + account + ' copied to clipboard.')
else:
    print ('There is no account named ' + account)

Path added添加的路径

Using Admin Command Prompt (some of these are typos - hit enter too fast)使用管理员命令提示符(其中一些是拼写错误 - 输入太快)

Looks like the problem is not with your "PATH".看起来问题不在于您的“PATH”。 It is the way you execute the file.这是您执行文件的方式。

Ideally, it should be,理想情况下,它应该是,

python [python-file-name].py [command-line-arguments] python [python 文件名].py [命令行参数]

In my case it was,在我的情况下,

python test.py email python test.py 电子邮件

In your case,在你的情况下,

python [your_file_name.py] [email|blog|luggage] python [your_file_name.py] [电子邮件|博客|行李]

This works like charm for me in both command line and IDE in both the usecases在这两个用例中,这对我来说在命令行和 IDE 中都很有用

ie with and without command line args.即有和没有命令行参数。 Just check if you are giving it the right way只需检查您是否以正确的方式进行操作

我执行的屏幕截图

Additional information :附加信息 :

From the screen shot, I see that you are using windows 10 (Correct me if am wrong).从屏幕截图中,我看到您使用的是 Windows 10(如果有误,请纠正我)。 In windows 10, you have 2 command prompts.在 Windows 10 中,您有 2 个命令提示符。

  1. One is for the regular users一种是普通用户
  2. The other one is in administrator mode.另一种是管理员模式。

python will not launch on regular command prompts. python 不会在常规命令提示符下启动。

I think you are using the regular mode command prompt.我认为您正在使用常规模式命令提示符。 Please use the command prompt with admin access.请使用具有管理员权限的命令提示符。

Check this link to see how to open cmd prompt as an admin.检查链接以查看如何以管理员身份打开 cmd 提示符。 I think you can also open by right clicking on the windows icon.我认为您也可以通过右键单击 Windows 图标来打开。

If you still wanna use the regular cmd prompt you may also try python -i (interactive mode)如果您仍想使用常规 cmd 提示符,您也可以尝试 python -i (交互模式)

This struck me as weird when I came across this example in the same book and I thought it was a one off but he keeps using them.当我在同一本书中遇到这个例子时,这让我感到很奇怪,我认为这是一个单独的例子,但他一直在使用它们。 The shebang line is wrong in the book.书中的shebang线是错误的。 It should be something like this:它应该是这样的:

#!/usr/bin/env python3

or或者

#!/usr/bin/python3

Or whatever your full path is to python3.或者无论你到 python3 的完整路径是什么。

A shebang line must be #! shebang 行必须是 #! and then whitespace and then a forward slash.然后是空格,然后是正斜杠。 No pathname expansion is done to find the interpreter (env or python3) so you must specify the full path.没有进行路径名扩展来查找解释器(env 或 python3),因此您必须指定完整路径。 The space and interpreter name without its full path just won't work.没有完整路径的空间和解释器名称将不起作用。 In Appendix B at the end of the book, there is a short section that explains how to setup the shebang line properly, but yet this is at the end of the book.在本书末尾的附录 B 中,有一小部分解释了如何正确设置 shebang 线,但这在本书的末尾。 He does mention it in the first example, but being a beginner book, I'm sure this threw more than a few people off.他在第一个例子中确实提到了它,但作为一本初学者的书,我相信这会让很多人失望。

Curiously in Appendix B he states:奇怪的是,他在附录 B 中指出:

On Windows, the shebang line is #!在 Windows 上,shebang 行是 #! python3.蟒蛇3。

This reflects the usage in the book, but as far as I know, shebang lines don't have any effect in Windows.这反映了书中的用法,但据我所知,shebang行在windows中没有任何作用。 Someone correct me if I'm wrong.如果我错了,有人纠正我。 I think the book should have used #!/usr/bin/env python3 in it's examples to save confusion.我认为这本书应该在它的例子中使用 #!/usr/bin/env python3 来避免混淆。

My issue likes yours, but this is what i tried, and it worked.我的问题喜欢你的,但这是我尝试过的,它奏效了。

First, in CMD, from any path, you can launch Python by typing python首先,在 CMD 中,从任何路径,您都可以通过键入 python 来启动 Python

like:喜欢:

c:\>python

This means your windows path is correct.这意味着您的 Windows 路径是正确的。

Second, try to change your folder directory to the python code folder, in my case, it is c:\my documents\PythonCode.其次,尝试将您的文件夹目录更改为 python 代码文件夹,在我的情况下,它是 c:\my files\PythonCode。 The command is:命令是:

cd "My Documents"\PythonCode

After this cmd, it should look like在这个 cmd 之后,它应该看起来像

C:\My Documents\PythonCode>

Last, type最后,输入

python test.py

This should execute your py script.这应该执行您的 py 脚本。

So, in your case, you can try this:所以,在你的情况下,你可以试试这个:

c: 
cd c:\"Python Scripts"
python pw.py email

first line sets your directory to c drive second line sets your directory to Python Scripts folder third line execute your pw.py script.第一行将您的目录设置为 c 驱动器第二行将您的目录设置为 Python Scripts 文件夹第三行执行您的 pw.py 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM