Hi everyone I am trying to create a log using win32com.client module - I have installed the module with the following command - pip install pywin32 - Installation was successful. However when I tried to run the below script I get the following error. The Python Win32 extensions for NT (service, event logging) appear not to be available.
``` logger = logging.getLogger('myapp')
``` hdlr = logging.handlers.NTEventLogHandler('My Application')
``` logger.addHandler(hdlr)
``` logger.setLevel(logging.INFO)
``` logger.error('We have a problem')
``` logger.info('Its five oclock now!')
``` logging.shutdown()
Any idea why that would happen?
Thanks in advance to anyone who is willing to help.
Cheers
This special handler has its own special requirements described in the documentation:
The NTEventLogHandler class, located in the logging.handlers module, supports sending logging messages to a local Windows NT, Windows 2000 or Windows XP event log. Before you can use it, you need Mark Hammond’s Win32 extensions for Python installed.
Note: Please put the triple backticks just around your whole code, not at the line beginning:
I am sorry I skimmed your introduction too quickly. Disclaimer: I have never used Windows-specific libraries in Python. It looks like that you installed the correct package:
Anyway it seems that in the linked code the first or the second import from these two failed:
import win32evtlogutil, win32evtlog
I suggest you the following steps:
Confirm that the import really fails
$ python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32evtlogutil
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'win32evtlogutil'
>>> import win32evtlog
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'win32evtlog'
Check if you have the package installed: python3 -m pip list -v With -v it will also show you the location where the package is installed.
Ideas of possible problems: Maybe you have multiple Python installations and the package is in the other one? Maybe you created a virtual environment (venv) which isolates installed packages?
Hi again and thanks so much for the detailed explanation.
Will try what you suggested and see what happens.
Once again thanks very much. You’re an absolute legend!
Cheers
PS as far as I can see I only have one Python installation. As for the virtual environment I doubt I created one as I don’t really know what this is. Or maybe I have created one without knowing when I went through the installation process. Will have to double check.
These are not packages to be installed. These are modules which according to the code I linked in my previous message seem to be expected to be present and the documentation suggests that they should be provided by the package pywin32. Which is listed as installed in the screenshot.
Available modules can be listed using this command: py -the_version -c "help('modules')" See below for information about the_version.
Please do not send text in pictures. It is hard to work with. Simply copy the text and paste it between triple backticks here (to prevent formatting):
```
your text
```
We need to see the exact import commands and error messages shown. The procedure is described below.
Next steps:
put here all the related commands you execute and their complete output:
List python installations on Windows: py -0p
The next steps execute for every installed python version:
For example in py -the_version -c "import win32evtlog" you will replace the_version
consecutively for all your versions, e.g: py -3.9-64 -c "import win32evtlog"
Test the importability: py -the_version -c "import win32evtlog" py -the_version -c "import win32evtlogutil"
List the installed packages and look for pywin32: py -the_version -m pip list -v
If you do not find version where imports do not fail, you can try to uninstall and reinstall the package: py -the_version -m pip install -U pip py -the_version -m pip uninstall pywin32 py -the_version -m pip install pywin32
Run your program with the same Python version: py -the_version your_program.py
Hi and thanks so much for your ongoing support. It’s much appreciated.
I will try all the commands you suggested and go from there.
I forgot to mention that I use pycharm ide to execute my code. I don’t directly code in the cmd prompt. Could this be the reason I’m getting the error? In other words, could it be possible that the modules are installed correctly, but pycharm does not see them and returns that “The Python Win32 extensions for NT (service, event logging) appear not to be available.” Message?
Sorry about photo and text. So just to clarify It’s not recommended to post screenshot? I’m only asking because in my previous post I uploaded a screenshot to show the installed modules. The text was not in picture originally, it was completely separate. Maybe that’s how it gets formatted when a post is written?