python - 在 Powershell 下运行 Python 脚本

标签 python 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 将其解释为错误条件。这在 more details 中讨论在前两个questions .没有看到实际的代码,很难说哪一部分写入了错误流。

作为变通方法,尝试重定向错误流。在 Powershell 中,2> $null redirects error streamnull。可以将 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/

相关文章:

c# - 如何在 PowerShell 或 C# 中获取进程的命令行信息

powershell - 有没有办法获得大于 int32 的数字范围?

python - 将 py.test 与已编译的库代码一起使用

python:在两个脚本之间发送信息

python - 安装多个 python 时将库保持在正确的位置

windows - 为什么这个简单的 Powershell 脚本在需要时不退出?

xml - 检索变量的XML内容

arrays - Powershell:将 pracl 命令的输出通过管道传输到数组

python - 如何在Python列表理解中翻译嵌套的if-else

Python:元素不是可点击的 Selenium