[英]Cant locate or run my script within Powershell
我正在做“Learn Python the Hard way”的练习 14,并且我已经写出了我的源代码,但我似乎无法在 PyCharm 或 Z531B84AD41B279007.3 的控制台中运行脚本我不知道如何使用,而且很迷茫。
我尝试打开 PowerShell 并将我的文件目录粘贴到那里,但我得到的只是一个错误提示
PS C:\Users\avalo> F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py
F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH : The term
'F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:1
+ F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (F:\Python_Proje...excercises\LPTH:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我不知道这意味着什么。 您可以在下面找到我的源代码。
from sys import argv
script, user_name = argv
prompt = '> '
print(f"Hi {user_name}, I'm the {script} scrpit.")
print("I'd like to ask you a few questions")
print(f"Do you like me {user_name}?")
likes = input(prompt)
print(f"Where do you live {user_name}?")
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer, Nice.
""")
当我尝试在 PyCharm 中运行脚本时,我得到了错误 -
Traceback (most recent call last):
File "C:/Users/avalo/PycharmProjects/LPTH Excercises/venv/LPTH ex14.py", line 3, in <module>
script, user_name = argv
ValueError: not enough values to unpack (expected 2, got 1).
让我们看一下命令和错误信息:
PS C:\Users\avalo> F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py
F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH : The term
'F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
因此,您在 Powershell 提示符中键入了F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py
Powershell 试图理解用户输入,但不能这样做。 你看,Powershell 认为只有一个命令和一个参数——因为有一个空格。 因此,Powershell的推理是这样的,
尝试执行F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH
并将ex14.py
作为参数传递给前面提到的 LPTH 东西。
这没有多大意义,也不存在F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH
这样的东西。 这就是为什么错误消息说它未被识别为 cmdlet、function、脚本文件或可运行程序的名称。
这种行为不是 Powershell 自己的怪癖。 Cmd shell、Bash 和许多其他需要特殊的解决方法来处理包含空格的文件名。
要解决此问题,您需要使用引号告诉 shell 空格是文件名的一部分,而不是分隔符。 此外,您应该将路径作为参数传递给python
。 实际路径取决于您的设置,但类似
& 'C:\Program Files (x86)\Python37-32\python' 'F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py'
符号是调用运算符,它将执行 python.exe。 请注意,parameter.py 文件用单引号括起来,以便将空格作为文件名包含在内。
故事的寓意:避免文件名中的空格,除非您只使用 GUI 工具。 可以使用下划线_
代替空格来保持可读性。 像LPTH_ex14.py
这样的文件名不需要任何引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.