Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6156505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:37:18+00:00 2026-05-23T20:37:18+00:00

I have a class that will normally run under Windows and uses the win32com

  • 0

I have a class that will normally run under Windows and uses the win32com module. I also have a mock version of this class, both for testing and for demo purposes, which imports from the original, and should be able to run anywhere. So, it’s like this, in two files:

import win32com.client
class basic():
    def local_instance(self):
        # called once during setup of execution
        res = win32com.client.Dispatch('Foo.Bar')
    def res_attrib(self):
        # called repeatedly by threads; must re-instantiate COM obj
        return win32com.client.Dispatch('Foo.Bar').Attribute
    def otherstuf(self):
        ...

=======================
from basic import basic

class mock(basic):
    def local_instance(self):
        res = mock_foobar()    # mock implementation with .Attribute
    def res_attrib(self):
        return res.Attribute   # mock implementation, this is OK
    # otherwise uses otherstuf() unchanged

The problem with this is when the mock version is loaded in an environment without win32com, the import win32com.client statement throws.

My question is, what’s the correct approach to limiting the application of that import?

  • embed the import inside each of the methods, where it will be executed repeatedly:

    def local_instance(self):
        import win32com.client
        res = win32com.client.Dispatch('Foo.Bar')
    def res_attrib(self):
        import win32com.client
        return win32com.client.Dispatch('Foo.Bar').Attribute
    
  • nest the import and the def’s inside a try block, where it will fail strangely if user tries to run basic in a Windows environment but doesn’t happen to have win32com installed:

    try:
        import win32com.client
        def local_instance(self):
            res = win32com.client.Dispatch('Foo.Bar')
        def res_attrib(self):
            return win32com.client.Dispatch('Foo.Bar').Attribute
    except:
        # alternate or no defs
    
  • or other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:37:19+00:00Added an answer on May 23, 2026 at 8:37 pm

    nest the import and the def’s inside a try block,

    This is the standard approach that most people use.

    Do this outside any class definitions. At the very top level with your ordinary imports. Once only.

    Use the two functions within any class definition which occurs after this in the module.

    try:
        import win32com.client
        def local_instance():
            res = win32com.client.Dispatch('Foo.Bar')
        def res_attrib():
            return win32com.client.Dispatch('Foo.Bar').Attribute
    except ImportError, why:
        # alternate or no defs
    

    where it will fail strangely if user tries to run basic in a Windows environment but doesn’t happen to have win32com installed:

    This is not true at all.

    First, you can examine the why variable.

    Second, you can examine os.name or platform.uname() to see if this is Win32 and alter the except block based on operating system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a class that will handle internal logging in my application. Now
I have written an apex class that will create a PDF quote and attach
We have a static method in a utility class that will download a file
If you have data for a class that will be modified and needs to
So let's assume I have a class named ABC that will have a list
I have a buffer in class 'bufferClass' that will generate a signal to tell
I'm building a class library that will have some public & private methods. I
I have a timeseries class that, over the course of a day will hold
If you have a large class that executes after output(headers/text) is already sent, will
I have some code that will be accessed from two threads: class Timer{ public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.