简体   繁体   English

无法在 Powershell 中找到或运行我的脚本

[英]Cant locate or run my script within Powershell

I'm doing exercise 14 of "Learn Python the Hard way" and i have written out my source code, but i can't seem to run the script in either the console within PyCharm, or Powershell.我正在做“Learn Python the Hard way”的练习 14,并且我已经写出了我的源代码,但我似乎无法在 PyCharm 或 Z531B84AD41B279007.3 的控制台中运行脚本I don't know how to use either and am rather lost.我不知道如何使用,而且很迷茫。

I tried opening PowerShell and pasting my directory for the file in there but all i got was an error saying我尝试打开 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

I have no idea what any of that means.我不知道这意味着什么。 Below you can find my source code.您可以在下面找到我的源代码。

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.
""")

When i tried to run the script within PyCharm I got the error -当我尝试在 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). 

Let's look at the command and error message:让我们看一下命令和错误信息:

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.

So, you typed F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py into a Powershell prompt.因此,您在 Powershell 提示符中键入了F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py Powershell tries to make sense of user input, but cannot do that. Powershell 试图理解用户输入,但不能这样做。 You see, Powershell thinks there were one command and one parameter - because there was a space.你看,Powershell 认为只有一个命令和一个参数——因为有一个空格。 Thus, Powershell's reasoning is like so,因此,Powershell的推理是这样的,

Try to execute F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH And pass ex14.py as a parameter to the LPTH thing mentioned earlier.尝试执行F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH并将ex14.py作为参数传递给前面提到的 LPTH 东西。

This doesn't make much sense, and there isn't such a thing as F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH .这没有多大意义,也不存在F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH这样的东西。 That's why the error message says that it wasn't recognized as as the name of a cmdlet, function, script file, or operable program.这就是为什么错误消息说它未被识别为 cmdlet、function、脚本文件或可运行程序的名称。

This kind of behavior isn't Powershell's own quirk.这种行为不是 Powershell 自己的怪癖。 Cmd shell, Bash and many other need special work-arounds to work with file names that contain spaces. Cmd shell、Bash 和许多其他需要特殊的解决方法来处理包含空格的文件名。

To work around the issue, you need to use quotes to tell the shell that space is part of file name, not a separator.要解决此问题,您需要使用引号告诉 shell 空格是文件名的一部分,而不是分隔符。 What's more, you should pass the path as an argument to python .此外,您应该将路径作为参数传递给python The actual path depends on your setup, but it's something akin实际路径取决于您的设置,但类似

& 'C:\Program Files (x86)\Python37-32\python' 'F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py'

The amperstand is the call operator which will execute python.exe.符号是调用运算符,它将执行 python.exe。 Note that the parameter.py file is enclosed in single quotes, so that the space will be included as a file name.请注意,parameter.py 文件用单引号括起来,以便将空格作为文件名包含在内。

Moral of the story: avoid space in file names, unless you are using GUI tools only.故事的寓意:避免文件名中的空格,除非您只使用 GUI 工具。 Underscore _ can be used instead of a space to maintain readability.可以使用下划线_代替空格来保持可读性。 A file name such as LPTH_ex14.py wouldn't need any quotes.LPTH_ex14.py这样的文件名不需要任何引号。

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

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