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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:36:03+00:00 2026-06-03T04:36:03+00:00

How can I import a python class but avoid running the import statement in

  • 0

How can I import a python class but avoid running the import statement in that module?

Module foo
    from bar import A

Module bar
    import alpha

    class A(object):
        ...

    class B(objects):
        ...

I want to import class A but don’t need class B. The import statement in the module bar is required for class B but I’d like to avoid needing to install that dependency if possible, as (I assume) it will be loaded into memory but not used.

Any help would be appreciated.

  • 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-06-03T04:36:05+00:00Added an answer on June 3, 2026 at 4:36 am

    You can’t stop bar from importing alpha without hacking around in its source. But you can “fake out” alpha, by writing it into sys.modules:

    >>> import alpha
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named alpha
    >>> import sys; sys.modules['alpha'] = object()
    >>> import alpha
    >>>
    

    This works because Python caches imported modules in sys.modules, so that if you import something twice you don’t have to go through all the hard work the second time. Adding alpha to it means that Python thinks you have already imported alpha, so when bar tries to do so it will just get back the cached copy.

    Obviously, you should think carefully about whether you are comfortable doing this — bar will crash in unexpected ways if it actually uses alpha anywhere.

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

Sidebar

Related Questions

I can import MySQLdb from the interactive python command line but Zope gives me
I know that Python, unlike Java, supports inheritance. But does a user class can
In Python you can do a: from a import b as c How would
For a class project I'm trying to do some socket programming Python but running
I can't seem to get Python to import a module in a subfolder. I
After I installed MySQLdb by sudo apt-get install python-mysqldb I can import and use
It is recommended to not to use import * in Python. Can anyone please
In Python, I can do this: >>> import string >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Is there
A user can import data into our website from a file. The data normally
To my surprise and delight I read that an adminsitrator can import (nearly directly)

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.