3

Why does running a python script under powershell with logging enabled show below (standard python logging module)

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

The script runs and does what is required and prints out all the required message but why the error? Would like to get rid of the error message.

Looks like a possible Execution-Policy but I tried running this at Administrator privileges with same result?

Python 3.8.0, Windows 10, Powershell 5.1

Sam
  • 97
  • 10

2 Answers2

1

Something in the Python script writes into stderr, which Powershell interprets as an error condition. This is discussed in more details in two previous questions. Without seeing the actual code, it's hard to say which part writes into error stream.

As a work-around, try to redirect error stream. In Powershell, 2> $null redirects error stream into null. It's possible to combine stderr and stdout with 2>&1.

Maybe invoking the script with redirection would work. Like so,

python "File Creation.py" $OracleDate 1> output.log 2>&1

It seems that one can redirect the stderr in Python, so it might be worth trying those solutions too. See all the answers, there are multiple approaches.

vonPryz
  • 22,996
  • 7
  • 54
  • 65
0

Thanks to vonPryz. Link to the second question had an answer that helped.

This is a bug under Powershell ISE but runs fine under powershell command-line (i.e. not ISE)

Sam
  • 97
  • 10