首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Powershell中找到或运行我的脚本

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

Stack Overflow用户
提问于 2019-10-08 03:42:16
回答 1查看 60关注 0票数 1

我正在做“艰难地学习Python”的练习14,我已经写出了我的源代码,但我似乎无法在PyCharm或Powershell的控制台中运行该脚本。我也不知道怎么用,我很迷茫。

我尝试打开PowerShell并将我的文件目录粘贴到其中,但得到的结果是一个错误消息

代码语言:javascript
复制
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

我不知道这是什么意思。下面你可以找到我的源代码。

代码语言:javascript
复制
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中运行脚本时,我得到了错误-

代码语言:javascript
复制
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). 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-08 04:20:54

让我们看一下命令和错误消息:

代码语言:javascript
复制
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、函数、脚本文件或可操作程序的名称的原因。

这种行为并不是Powershell自己的怪癖。Cmd shell、Bash和其他许多工具都需要特殊的变通方法来处理包含空格的文件名。

要解决此问题,您需要使用引号告诉shell,空格是文件名的一部分,而不是分隔符。更重要的是,您应该将路径作为参数传递给python。实际路径取决于您的设置,但它类似于

代码语言:javascript
复制
& 'C:\Program Files (x86)\Python37-32\python' 'F:\Python_Projets\Learning_Python_the_hard_way_excercises\LPTH ex14.py'

amperstand是执行python.exe的呼叫操作员。请注意,参数.py文件用单引号括起来,因此空格将作为文件名包括在内。

这个故事的寓意是:避免在文件名中使用空格,除非您只使用GUI工具。可以使用下划线_而不是空格来保持可读性。像LPTH_ex14.py这样的文件名不需要任何引号。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58276158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档