[英]Running Python Script under Powershell
為什么在啟用日志記錄的情況下在 powershell 下運行 python 腳本如下所示(標准 python 日志記錄模塊)
python : INFO:root:File Creation Started at 18-Aug-2020
At C:\Untitled1.ps1:17 char:5
+ python "File Creation.py" $OracleDate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (INFO:root:XXXX... at 18-Aug-2020:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
INFO:root:Running query SELECT * FROM reporting
INFO:root:18216 records saved to .txt
INFO:root:Running query SELECT * FROM reporting
INFO:root:18216 records saved to .txt
INFO:root:Running query SELECT * FROM reporting.
INFO:root:11322 records saved to .txt
INFO:root:Running query SELECT * from reporting
INFO:root:18216 records saved to .txt
INFO:root:Running query SELECT * FROM reporting
INFO:root:18216 records saved to .txt
該腳本運行並執行所需的操作並打印出所有必需的消息,但為什么會出現錯誤? 想擺脫錯誤信息。
看起來像一個可能的執行策略,但我嘗試以管理員權限運行它並得到相同的結果?
Python 3.8.0, Windows 10, Powershell 5.1
Python 腳本中的某些內容寫入stderr
,Powershell 將其解釋為錯誤條件。 這在前面的兩個問題中有更詳細的討論。 在沒有看到實際代碼的情況下,很難說是哪一部分寫入了錯誤 stream。
作為解決方法,嘗試重定向錯誤 stream。在 Powershell 中, 2> $null
將錯誤 stream 重定向到null
。 可以將 stderr 和 stdout 與2>&1
結合使用。
也許使用重定向調用腳本會起作用。 像這樣,
python "File Creation.py" $OracleDate 1> output.log 2>&1
似乎可以在 Python 中重定向 stderr ,因此也值得嘗試這些解決方案。 查看所有答案,有多種方法。
感謝 vonPryz。 鏈接到第二個問題的答案很有幫助。
這是 Powershell ISE 下的錯誤,但在 powershell 命令行(即不是 ISE)下運行良好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.