How does Python get the log log of exE software output - python

The exe file is also written in Python, which USES Pyinstaller to convert to exe. This EXE outputs warninglevel logs at the end. For example, Logger.warning (" Hello World ") outputs "Hello World". So how do I get this "Hello World" when the main thread calls this exe subroutine?
This is the code for logTest.exe
import logging
logger = logging.getLogger(__name__)
logger.warning("Something maybe fail.")
This is the code for output.py
import os
import subprocess
import logging
main=r"D:\python\commendTest\dist\logTest.exe"
f=os.popen(main)
data=f.readlines()
f.close()
log:
Something maybe fail.
But I want to get "Something maybe fail." into the string

Related

2-way interop/IPC between SWI Prolog & Python

There is module to call from Python (pyswip) =to=> Prolog, but is there a way to call the other way around from Prolog =to=> Python ?
I need it so I can call Spacy-NLP module from SWI-Prolog
couldn't find if SWI supports ZeroMQ.
This is the sample python code in a file "c:\hello.py" that extracts the argumentq on the command line (optional). It's results are echoed to the standard output stream.
import sys
def hello():
return 'hello world: ' + ','.join(sys.argv[1:])
if __name__ == '__main__':
print(hello())
Here is the prolog program in file "c:\hello.pl" invoking the above python code as a process:
do :-
process_create(path('python3.7'),
['c:/hello.py', foo, bar],
[stdout(pipe(In))]), %output stream named In
read_string(In, Len, X), %In=input stream to read_string/3
write(X), nl, halt.
To activate this prolog/python combo and writting the results to the output stream
$ swipl -g do c:\hello.pl
hello world: foo,bar
Does this do what you wanted?

Why does Win 7 make a beep when I run a Python script?

I have seen a few question asking how to make a Python script that makes a beep sound.
This question is not about that.
When I run certain Python scripts Windows 7 makes a beep sound but I want to stop the beep.. or at least understand why it's happening.
Here is a test script - test.py:
#!/usr/bin/python
# importing modules
import os
import time
import datetime
from datetime import datetime
def f_log(strtext):
# In this method try and write to the log file
logFilePath = '/home/osmc/python/pyscripter_file.txt'
ts_local = datetime.now()
ts = ts_local.strftime("%d/%b/%Y %H:%M:%S")
try:
# Check if the logfile exists
if(os.path.isfile(logFilePath)):
# Append the error message to the log file. 'a+' creates the file if it does not exist.
with open(logFilePath, 'a+') as f:
f.write(ts + '\t' + strtext + '\n')
else:
print("Log File does not exist - Creating File now")
with open(logFilePath, 'a+') as f:
f.write(ts + '\t' + 'Log File does not exist - Creating File now' + '\n')
f.write(ts + '\t' + strtext + '\n')
except IOError as e:
# Handle the exception
print("Error Msg")
f_log("Test Script")
print("Hello World")
This script is in a directory on a Raspberry Pi running OSMC... in a folder called /home/osmc/python
I have opened it as a remote file in the Pyscripter IDE and can see ssh://osmc//home/osmc/python/test.py
So this means, the Pyscripter IDE, running on Windows 7, is running the Python script on the remote machine. When I run the script this way I get a beep when it finishes executing.
If I create a crontab to run it in osmc...
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin/python
53 22 * * * /usr/bin/python /home/osmc/python/test.py >> out.txt 2>&1
Then the script also gives a beep at the end of execution.
If I run the script from a command line to osmc (from an ssh connection in Putty)
# cd /home/osmc/python
# python test.py
I DO NOT get a beep when it completes.
If I run this next script in Pyscripter (or any other way) it does NOT make a beep.
import sys
print('Python '+sys.version.replace('\n','')+' at '+sys.executable+' on '+sys.platform)
Why do I sometimes get the beep? Something in the Python code in the first script I guess?
Flex
My test Python script writes to a log file. I had that log file open in PyScripter IDE.
PyScripter IDE by design makes a beep to signal a File Change Notification affecting any files open in the IDE. This is what was causing the beep whether I was running the script from within PyScripter or not I was still getting the beep because the script was writing to the file open in PyScripter IDE.
I asked the developer and it is currently not possible to turn off this beep.
Flex

Debug can't print results to file in python3

i'm using python 3.8 on ubuntu 20.04, and I cant get the DEBUG statements to log to my other file, my code -
import logging
logging.basicConfig(level=logging.DEBUG, filename = 'logDEBUG.txt')
def say(x):
print(x)
phrase = 'hi'
logging.debug(say(phrase))
You need to return the value, not print it.
import logging
logging.basicConfig(level=logging.DEBUG, filename = 'logDEBUG.txt')
def say(x):
return x
phrase = 'hi'
logging.debug(say(phrase))
Result in logDEBUG.txt:
DEBUG:root:hi
Do you have the problem that the file is not created?
If so check to see what your python path is, i don't know if you expect that the log file should appear in the same folder as your script.
Search for the file on your computer and see where it ends up.

I keep having a error when running the script. It keeps sending "name input_file is not defined"

I am trying to create a python def to unzip a few .gz files within a folder. I know that the main script works if it is not in the def. The script I have created is similar to others I have done but this one give me the error
File "unzip.py", line 24, in
decompressed_files(input_folder)
NameError: name 'input_folder' is not defined
I copied the script below so someone can help me to see where the error is. I haven't done any BioInformatics for the last couple of years and I am a bit rusty.
import glob
import sys
import os
import argparse
import subprocess
import gzip
def decompressed_files(input_folder):
print ('starting decompressed_files')
output_folder=input_folder + '/fasta_files'
if os.path.exists(output_folder):
print ('folder already exists')
else:
os.makedirs(output_folder)
for f in input_folder:
fastqs=glob.glob(input_folder + '/*.fastq.gz')
cmd =[gunzip, -k, fastqs, output_folder]
my_file=subprocess.Popen(cmd)
my_file.wait
print ('The programme has finished doing its job')
decompressed_files(input_folder)
This is done for python 2.7, I know that is old but it is the one that it is installed in my work server.
That's why when you call decompressed_files(input_folder) in the last line, you didn't define input_folder before. you should do it like this :
input_folder = 'C:/Some Address/'
decompressed_files(input_folder)

NTEventLogHandler from a Python executable

import logging, logging.handlers
def main():
ntl = logging.handlers.NTEventLogHandler("Python Logging Test")
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
logger.addHandler(ntl)
logger.error("This is a '%s' message", "Error")
if __name__ == "__main__":
main()
The Python (2.7.x) script above writes "This is a 'Error' message" to the Windows Event Viewer. When I run it as a script, I get the expected output. If I convert the script to an executable via PyInstaller, I get an entry in the event log but it says something completely different.
The description for Event ID ( 1 ) in Source ( Python Logging Test ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: This is a 'Error' message.
This is the command I use to convert the script into an executable: pyinstaller.py --onefile --noconsole my_script.py though the command line parameters do not appear to have any impact on this behaviour and it will suffice to just call pyinstaller.py my_script.py.
I would appreciate any help in understanding what is going on and how I go about fixing this.
Final solution
I didn't want to go down the resource hacker route, as that is going to be a difficult step to automate. Instead, the approach I took was to grab the win32service.pyd file from c:\Python27\Lib\site-packages\win32 and place it next to my executable. The script was then modified pass the full path to the copy of the win32service.pyd file and this works in both script and exe form. The final script is included below:
import logging, logging.handlers
import os
import sys
def main():
base_dir = os.path.dirname(sys.argv[0])
dllname = os.path.join(base_dir, "win32service.pyd")
ntl = logging.handlers.NTEventLogHandler("Python Logging Test", dllname=dllname)
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
logger.addHandler(ntl)
logger.error("This is a '%s' message", "Error")
if __name__ == "__main__":
main()
Usually, the Windows Event Log doesn't store error messages in plain text, but rather message ID references and insertion strings.
Instead of storing a message like Service foo crashed unexpectedly, it stores a message ID which points to a resource string stored in a DLL. In this case, the resource would be something like Service %s crashed unexpectedly and foo would be stored as insertion string. The program which writes the message registers the resource DLL.
The reason for this is localization. DLLs can store lots of different resources (dialog layout, strings, icons…), and one DLL can contain the same resource in many different languages. The operating system automatically chooses the right resources depending on the system locale. Resource DLLs are used by virtually all Microsoft utilities and core utilities.
Side note: Nowadays, the preferred (and cross-platform) way for localization is gettext.
This is used for the message log as well – ideally, you could open a log from an German Windows installation on an English one with all messages in English.
I suspect that the pywin32 implementation skips that mechanism by only having one single message ID (1) which is just something like "%s". It is stored in win32service.pyd and registered by pywin32. This works fine as long as this file exists on the file system, but breaks as soon as it's hidden inside a PyInstaller executable. I guess you have to embed the message ID into your executable directly.
Edit: suspicion confirmed, the message table is indeed stored inside win32service.pyd
Resource Hacker showing the message table http://media.leoluk.de/evlog_rh.png
Try to copy the message table resource from win32service.pyd to your PyInstaller executable (for example using Resource Hacker).
Looking at the logging handler implementation, this might work:
def __init__(self, appname, dllname=None, logtype="Application"):
logging.Handler.__init__(self)
try:
import win32evtlogutil, win32evtlog
self.appname = appname
self._welu = win32evtlogutil
if not dllname:
dllname = os.path.split(self._welu.__file__)
dllname = os.path.split(dllname[0])
dllname = os.path.join(dllname[0], r'win32service.pyd')
You'd have to set dllname to os.path.dirname(__file__). Use something like this if you want it to continue working for the unfrozen script:
if getattr(sys, 'frozen', False):
dllname = None
elif __file__:
dllname = os.path.dirname(__file__)
ntl = logging.handlers.NTEventLogHandler("Python Logging Test", dllname=dllname)

Categories