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

  • SEARCH
  • Home
  • 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 3607000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:19:14+00:00 2026-05-18T21:19:14+00:00

I am trying to get a module to import, but only if an object

  • 0

I am trying to get a module to import, but only if an object of a specific class is called. For example:

class One(object):
    try:
        import OneHelper
    except ImportError:
        pass
    def __init__(self):
        # this function doesn't use OneHelper
        ...

    def blah(self):
        # this function does
        OneHelper.blah()

This causes a NameError: global name 'OneHelper' is not defined when the One.blah() function is called. So far the only thing I have found that works is importing the module into the actual functions that use it. So:

class One(object):
    def __init__(self):
        # this function doesn't use OneHelper
        ...

    def blah(self):
        try:
            import OneHelper
        except ImportError:
            pass
        # this function does
        OneHelper.blah()

But I don’t want to have to import the module in each function I want to use it in, I want it to be available to the whole class, but only if an instance of that class is instantiated. Apologies if I’m not being clear enough…

  • 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-18T21:19:15+00:00Added an answer on May 18, 2026 at 9:19 pm

    The import OneHelper works fine in the class, making it a class attribute. You can verify this with dir(One) after defining your class — there’s your OneHelper attribute. One.OneHelper is a reference to the module. In an instance, of course, you may access it as self.OneHelper from your methods. (You could also continue to access it as One.OneHelper.)

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

Sidebar

Related Questions

I'm trying to use Asp.NET WebAPI module but I get a weird error. When
i have to get static information from one 'module' to another. I'm trying to
Im trying to import a company module into my software and I get the
I'm trying to get to run a simple RBAC example with EJBs but when
I am trying to run the following Powershell script. import-module ActiveDirectory $computers = Get-ADComputer
I've been trying to get this module to work and no matter what I've
I'm writing a Joomla 2.5.3 module. I'm trying to get an article from a
I am trying to use python's telnetlib module to get information from a remote
So, I'm trying to use the Python Wave module to get an audio file
Here is my source code I'm trying to get to work: In Main.hs: import

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.