Win32com.client

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:

``` python
# your whole code
```
1 Like

Hi thanks so much for the suggestion.

As mentioned in my original post, I have installed the Win32 extension. Or maybe it is a different one that you are referring to?

Please bear with me as I have literally just started a couple of weeks ago with programming.

Cheers

Ps sorry re writing code - got it now.

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:

You can install pywin32 via pip: pip install pywin32

There seems to be a bug in the Python’s logging module. Instead of failing it just prints the error message to stdout! cpython/Lib/logging/handlers.py at 3.10 · python/cpython · GitHub

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:

  1. 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'
  1. 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.
  2. 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?
1 Like

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.


So I checked and here is the results. Based on the screenshot would you say that win32evtlogutil and win32evtlog are installed or not?

Also, I noticed that when I try to import both the interpreter highlights them in red as if it does not recognise them

Many thanks in advance

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:

  1. 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"
  2. Test the importability:
    py -the_version -c "import win32evtlog"
    py -the_version -c "import win32evtlogutil"
  3. List the installed packages and look for pywin32:
    py -the_version -m pip list -v
  4. 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
  5. Run your program with the same Python version:
    py -the_version your_program.py
2 Likes

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?

Correct and here’s why.

You’re not the first and I’m sure you won’t be the last :slight_smile:

2 Likes

Hey thanks so much for the info. I’m new around here apologies :slight_smile:

1 Like

It’s not a worry man – we all have to learn and that’s what it’s about, right?

1 Like

Thanks man I appreciate it. Trying my best. I’m hoping to get it right next time lol