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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:35:17+00:00 2026-05-22T18:35:17+00:00

I have an application that reads test scripts in python and sends them across

  • 0

I have an application that reads test scripts in python and sends them across the network for execution on a remote python instance. As the controlling program does not need to run these scripts I do not want to have all the modules the test scripts use installed on the controller’s python environment. However the controller does need information from the test script to tell it how to run the test.
Currently what I do to read and import test script data is something like

with open( 'test.py', 'r' ) as f:
    source = f.read()

m = types.ModuleType( "imported-temp", "Test module" )
co = compile( source, 'test.py', 'exec' )

exec co in m.__dict__

which yields a new module that contains the test. Unfortunately exec will raise ImportErrors if the test tries to import something the controller does not have. And worse, the module will not be fully imported.

If I can guarantee that the controller will not use the missing modules, is there someway I can ignore these exceptions? Or some other way to find out the names and classes defined in the test?

Examples test:

from controller import testUnit
import somethingThatTheControllerDoesNotHave

_testAttr = ['fast','foo','function']

class PartOne( testUnit ):
    def run( self ):
        pass

What the controller needs to know is the data in _testAttr and the name of all class definitions inheriting from testUnit.

  • 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-22T18:35:17+00:00Added an answer on May 22, 2026 at 6:35 pm

    Write an import hook that catches the exception and returns a dummy module if the module doesn’t exist.

    import __builtin__
    from types import ModuleType
    
    class DummyModule(ModuleType):
        def __getattr__(self, key):
            return None
        __all__ = []   # support wildcard imports
    
    def tryimport(name, globals={}, locals={}, fromlist=[], level=-1):
        try:
            return realimport(name, globals, locals, fromlist, level)
        except ImportError:
            return DummyModule(name)
    
    realimport, __builtin__.__import__ = __builtin__.__import__, tryimport
    
    import sys   # works as usual
    import foo   # no error
    
    from bar import baz     # also no error
    from quux import *      # ditto
    

    You could also write it to always return a dummy module, or to return a dummy module if the specified module hasn’t already been loaded (hint: if it’s in sys.modules, it has already been loaded).

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

Sidebar

Related Questions

I have an application that reads a CSV file with piles of data rows.
I have an application that reads a database and outputs alerts to any dependencies
I have an application that reads a table from a database. I issue an
I have an application that reads lines from a file and runs its magic
I have an application that reads data from a com port using javax.comm. The
I have an iPhone application that reads RSS feeds from a YouTube channel. However,
I have a Silverlight application that reads its content from an XML file. The
I have a windows forms application that reads and updates an XML file with
I have a basic C# console application that reads a text file (CSV format)
Problem in words: For my application, I have a class that reads from a

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.