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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:52:34+00:00 2026-06-06T23:52:34+00:00

I need to make a copy of a socket module to be able to

  • 0

I need to make a copy of a socket module to be able to use it and to have one more socket module monkey-patched and use it differently.

Is this possible?

I mean to really copy a module, namely to get the same result at runtime as if I’ve copied socketmodule.c, changed the initsocket() function to initmy_socket(), and installed it as my_socket extension.

  • 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-06T23:52:35+00:00Added an answer on June 6, 2026 at 11:52 pm

    You can always do tricks like importing a module then deleting it from sys.modules or trying to copy a module. However, Python already provides what you want in its Standard Library.

    import imp # Standard module to do such things you want to.
    
    # We can import any module including standard ones:
    os1=imp.load_module('os1', *imp.find_module('os'))
    
    # Here is another one:
    os2=imp.load_module('os2', *imp.find_module('os'))
    
    # This returns True:
    id(os1)!=id(os2)
    

    Python3.3+

    imp.load_module is deprecated in python3.3+, and recommends the use of importlib

    #!/usr/bin/env python3
    
    import sys
    import importlib.util
    
    SPEC_OS = importlib.util.find_spec('os')
    os1 = importlib.util.module_from_spec(SPEC_OS)
    SPEC_OS.loader.exec_module(os1)
    sys.modules['os1'] = os1
    
    os2 = importlib.util.module_from_spec(SPEC_OS)
    SPEC_OS.loader.exec_module(os2)
    sys.modules['os2'] = os2
    del SPEC_OS
    
    assert os1 is not os2, \
        "Module `os` instancing failed"
    

    Here, we import the same module twice but as completely different module objects. If you check sys.modules, you can see two names you entered as first parameters to load_module calls. Take a look at the documentation for details.

    UPDATE:

    To make the main difference of this approach obvious, I want to make this clearer: When you import the same module this way, you will have both versions globally accessible for every other module you import in runtime, which is exactly what the questioner needs as I understood.

    Below is another example to emphasize this point.

    These two statements do exactly the same thing:

    import my_socket_module as socket_imported
    
    socket_imported = imp.load_module('my_socket_module',
        *imp.find_module('my_socket_module')
    )
    

    On second line, we repeat ‘my_socket_module’ string twice and that is how import statement works; but these two strings are, in fact, used for two different reasons.

    Second occurrence as we passed it to find_module is used as the file name that will be found on the system. The first occurrence of the string as we passed it to load_module method is used as system-wide identifier of the loaded module.

    So, we can use different names for these which means we can make it work exactly like we copied the python source file for the module and loaded it.

    socket = imp.load_module('socket_original', *imp.find_module('my_socket_module'))
    socket_monkey = imp.load_module('socket_patched',*imp.find_module('my_socket_module'))
    
    def alternative_implementation(blah, blah):
        return 'Happiness'
    
    socket_monkey.original_function = alternative_implementation
    
    import my_sub_module
    

    Then in my_sub_module, I can import ‘socket_patched’ which does not exist on system! Here we are in my_sub_module.py.

    import socket_patched
    socket_patched.original_function('foo', 'bar')
    # This call brings us 'Happiness'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Image. I need to make a exactly copy of it and
I made this constructor, and I need to make a deep copy of it.
I need to make a script to copy one particular database role from one
I need to make a batch file that can copy files from one path
I have a char** color; I need to make a copy of the value
I need to make a copy of a given date 100s of times (I
I need to make a local copy of a database in a .NET app
I need make all of my posts update. I use bulk upload for store,
I need to make a moderate system like the one in fmylife.com. Basically the
I need to make a series (1-20) ajax calls and I need to have

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.