簡體   English   中英

無法在 Powershell 中找到或運行我的腳本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM