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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:21:41+00:00 2026-05-24T03:21:41+00:00

How can a Python program easily import a Python module from a file with

  • 0

How can a Python program easily import a Python module from a file with an arbitrary name?

The standard library import mechanism doesn’t seem to help. An important constraint is that I don’t want an accompanying bytecode file to appear; if I use imp.load_module on a source file named foo, a file named fooc appears, which is messy and confusing.

The Python import mechanism expects that it knows best what the filename will be: module files are found at specific filesystem locations, and in particular that the filenames have particular suffixes (foo.py for Python source code, etc.) and no others.

That clashes with another convention, at least on Unix: files which will be executed as a command should be named without reference to the implementation language. The command to do “foo”, for example, should be in a program file named foo with no suffix.

Unit-testing such a program file, though, requires importing that file. I need the objects from the program file as a Python module object, ready for manipulation in the unit test cases, exactly as import would give me.

What is the Pythonic way to import a module, especially from a file whose name doesn’t end with .py, without a bytecode file appearing for that import?

  • 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-24T03:21:42+00:00Added an answer on May 24, 2026 at 3:21 am

    My best implementation so far is (using features only in Python 2.6 or later):

    import os
    import sys
    import imp
    import contextlib
    
    @contextlib.contextmanager
    def preserve_value(namespace, name):
        """ A context manager to preserve, then restore, the specified binding.
    
            :param namespace: The namespace object (e.g. a class or dict)
                containing the name binding.
            :param name: The name of the binding to be preserved.
            :yield: None.
    
            When the context manager is entered, the current value bound to
            `name` in `namespace` is saved. When the context manager is
            exited, the binding is re-established to the saved value.
    
            """
        saved_value = getattr(namespace, name)
        yield
        setattr(namespace, name, saved_value)
    
    
    def make_module_from_file(module_name, module_filepath):
        """ Make a new module object from the source code in specified file.
    
            :param module_name: The name of the resulting module object.
            :param module_filepath: The filesystem path to open for
                reading the module's Python source.
            :return: The module object.
    
            The Python import mechanism is not used. No cached bytecode
            file is created, and no entry is placed in `sys.modules`.
    
            """
        py_source_open_mode = 'U'
        py_source_description = (".py", py_source_open_mode, imp.PY_SOURCE)
    
        with open(module_filepath, py_source_open_mode) as module_file:
            with preserve_value(sys, 'dont_write_bytecode'):
                sys.dont_write_bytecode = True
                module = imp.load_module(
                        module_name, module_file, module_filepath,
                        py_source_description)
    
        return module
    
    
    def import_program_as_module(program_filepath):
        """ Import module from program file `program_filepath`.
    
            :param program_filepath: The full filesystem path to the program.
                This name will be used for both the source file to read, and
                the resulting module name.
            :return: The module object.
    
            A program file has an arbitrary name; it is not suitable to
            create a corresponding bytecode file alongside. So the creation
            of bytecode is suppressed during the import.
    
            The module object will also be added to `sys.modules`.
    
            """
        module_name = os.path.basename(program_filepath)
    
        module = make_module_from_file(module_name, program_filename)
        sys.modules[module_name] = module
    
        return module
    

    This is a little too broad: it disables bytecode file generation during the entire import process for the module, which means other modules imported during that process will also not have bytecode files generated.

    I’m still looking for a way to disable bytecode file generation for only the specified module file.

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

Sidebar

Related Questions

If all tables I want to delete from have the column gamer_id can i
I'm trying to build a C++ extension for python using swig. I've followed the
Can i get the source code for a WAMP stack installer somewhere? Any help
I need to solve the following question which i can't get to work by
I'm in the process of porting some code from Linux to Mac OS X.
I want to use a temp directory that will be unique to this build.
Every time that I want to do a Layout, I'm getting a black layout
i have a input tag which is non editable, but some times i need
I have a new web app that is packaged as a WAR as part
We manage a site for a medical charity. They have a number of links

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.