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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:43:12+00:00 2026-05-27T03:43:12+00:00

I wrote a C extension (mycext.c) for Python 3.2. The extension relies on constant

  • 0

I wrote a C extension (mycext.c) for Python 3.2. The extension relies on constant data stored in a C header (myconst.h). The header file is generated by a Python script. In the same script, I make use of the recently compiled module. The workflow in the Python3 myscript (not shown completely) is as follows:

configure_C_header_constants() 
write_constants_to_C_header() # write myconst.h
os.system('python3 setup.py install --user') # compile mycext
import mycext
mycext.do_stuff()

This works perfectly fine the in a Python session for the first time. If I repeat the procedure in the same session (for example, in two different testcases of a unittest), the first compiled version of mycext is always (re)loaded.

How do I effectively reload a extension module with the latest compiled version?

  • 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-27T03:43:12+00:00Added an answer on May 27, 2026 at 3:43 am

    You can reload modules in Python 3.x by using the imp.reload() function. (This function used to be a built-in in Python 2.x. Be sure to read the documentation — there are a few caveats!)

    Python’s import mechanism will never dlclose() a shared library. Once loaded, the library will stay until the process terminates.

    Your options (sorted by decreasing usefulness):

    1. Move the module import to a subprocess, and call the subprocess again after recompiling, i.e. you have a Python script do_stuff.py that simply does

      import mycext
      mycext.do_stuff()
      

      and you call this script using

      subprocess.call([sys.executable, "do_stuff.py"])
      
    2. Turn the compile-time constants in your header into variables that can be changed from Python, eliminating the need to reload the module.

    3. Manually dlclose() the library after deleting all references to the module (a bit fragile since you don’t hold all the references yourself).

    4. Roll your own import mechanism.

      Here is an example how this can be done. I wrote a minimal Python C extension mini.so, only exporting an integer called version.

      >>> import ctypes
      >>> libdl = ctypes.CDLL("libdl.so")
      >>> libdl.dlclose.argtypes = [ctypes.c_void_p]
      >>> so = ctypes.PyDLL("./mini.so")
      >>> so.PyInit_mini.argtypes = []
      >>> so.PyInit_mini.restype = ctypes.py_object 
      >>> mini = so.PyInit_mini()
      >>> mini.version
      1
      >>> del mini
      >>> libdl.dlclose(so._handle)
      0
      >>> del so
      

      At this point, I incremented the version number in mini.c and recompiled.

      >>> so = ctypes.PyDLL("./mini.so")
      >>> so.PyInit_mini.argtypes = []
      >>> so.PyInit_mini.restype = ctypes.py_object 
      >>> mini = so.PyInit_mini()
      >>> mini.version
      2
      

      You can see that the new version of the module is used.

      For reference and experimenting, here’s mini.c:

      #include <Python.h>
      
      static struct PyModuleDef minimodule = {
         PyModuleDef_HEAD_INIT, "mini", NULL, -1, NULL
      };
      
      PyMODINIT_FUNC
      PyInit_mini()
      {
          PyObject *m = PyModule_Create(&minimodule);
          PyModule_AddObject(m, "version", PyLong_FromLong(1));
          return m;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote jquery custom fn extension script to remove the given element in an
I wrote a custom wysiwyg extension. I noticed that when it is generated it
I wrote a Python extension in C, and my python program uses that extension.
So I'm attempting to port a python C-extension I wrote for handling junctions/symbolic links/etc
I wrote a custom ordering LINQ extension method as below but I think it
We wrote a small Windows class library that implements extension methods for some standard
I recently found out about C# extension methods and wrote this one: /// <summary>
I set up Open-xchage(wrote on Java) on openSUSE and installed SOAP extension on it.
I'm trying to write an extension method to insert data into a dictionary of
I wrote an extension in C++ that uses libtidy, and it runs perfectly under

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.