为什么在启用日志记录的 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 将其解释为错误条件。这在 more details 中讨论在前两个questions .没有看到实际的代码,很难说哪一部分写入了错误流。
作为变通方法,尝试重定向错误流。在 Powershell 中,2> $null
redirects error stream为 null
。可以将 stderr 和 stdout 与 2>&1
结合使用。
也许使用重定向调用脚本会起作用。像这样,
python "File Creation.py" $OracleDate 1> output.log 2>&1
好像可以redirect the stderr在 Python 中,因此也值得尝试这些解决方案。查看所有答案,有多种方法。
关于python - 在 Powershell 下运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63463471/