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 6098189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:47+00:00 2026-05-23T13:07:47+00:00

Quick background: writing a module. One of my objects has methods that may or

  • 0

Quick background: writing a module. One of my objects has methods that may or may not be successfully completed – depending on the framework used underneath my module. So a few methods first need to check what framework they actually have under their feet. Current way of tackling this is:

def framework_dependent_function():
    try:
        import module.that.may.not.be.available
    except ImportError:
        # the required functionality is not available
        # this function can not be run
        raise WrongFramework 
        # or should I just leave the previous exception reach higher levels?

[ ... and so on ... ]

Yet something in my mind keeps telling me that doing imports in the middle of a file is a bad thing. Can’t remember why, can’t even come up with a reason – apart from slightly messier code, I guess.

So, is there anything downright wrong about doing what I’m doing here? Perhaps other ways of scouting what environment the module is running in, somewhere near __init__?

  • 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-23T13:07:47+00:00Added an answer on May 23, 2026 at 1:07 pm

    This version may be faster, because not every call to the function needs to try to import the necessary functionality:

    try:
        import module.that.may.not.be.available
        def framework_dependent_function():
            # whatever
    except ImportError:
        def framework_dependent_function():
            # the required functionality is not available
            # this function can not be run
            raise NotImplementedError
    

    This also allows you to do a single attempt to import the module, then define all of the functions that might not be available in a single block, perhaps even as

    def notimplemented(*args, **kwargs):
        raise NotImplementedError
    fn1 = fn2 = fn3 = notimplemented
    

    Put this at the top of your file, near the other imports, or in a separate module (my current project has one called utils.fixes). If you don’t like function definition in a try/except block, then do

    try:
        from module.that.may.not.be.available import what_we_need
    except ImportError:
        what_we_need = notimplemented
    

    If these functions need to be methods, you can then add them to your class later:

    class Foo(object):
        # assuming you've added a self argument to the previous function
        framework_dependent_method = framework_dependent_function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question. There is a legacy website (that is not under my control and
Quick background info: of the three database tables that are relevant for this question,
I'm writing a quick plugin (one of my first) to help with buttons if
Quick background I have a concurrent map I used to cache some values that
Real quick background : We have a PDFMaker (HTMLDoc) that converts html into a
So the quick background is that I have a page listing a set of
heres the quick background of the app Basically.. Multiple users(usually only one) per machine
This may be a silly question but I was writing a quick test page
This may not be possible, but I have a DIV that I am transforming
Quick background: I'm programming in PHP, I have a domain model with a separate

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.