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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:14:02+00:00 2026-05-27T02:14:02+00:00

I have a pure C module for Python and I’d like to be able

  • 0

I have a pure C module for Python and I’d like to be able to invoke it using the python -m modulename approach. This works fine with modules implemented in Python and one obvious workaround is to add an extra file for that purpose. However I really want to keep things to my one single distributed binary and not add a second file just for this workaround.

I don’t care how hacky the solution is.

If you do try to use a C module with -m then you get an error message No code object available for <modulename>.

  • 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-27T02:14:03+00:00Added an answer on May 27, 2026 at 2:14 am

    -m implementation is in runpy._run_module_as_main . Its essence is:

    mod_name, loader, code, fname = _get_module_details(mod_name)
    <...>
    exec code in run_globals
    

    A compiled module has no “code object” accociated with it so the 1st statement fails with ImportError("No code object available for <module>"). You need to extend runpy – specifically, _get_module_details – to make it work for a compiled module. I suggest returning a code object constructed from the aforementioned "import mod; mod.main()":
    (python 2.6.1)

        code = loader.get_code(mod_name)
        if code is None:
    +       if loader.etc[2]==imp.C_EXTENSION:
    +           code=compile("import %(mod)s; %(mod)s.main()"%{'mod':mod_name},"<extension loader wrapper>","exec")
    +       else:
    +           raise ImportError("No code object available for %s" % mod_name)
    -       raise ImportError("No code object available for %s" % mod_name)
        filename = _get_filename(loader, mod_name)
    

    (Update: fixed an error in format string)

    Now…

    C:\Documents and Settings\Пользователь>python -m pythoncom
    
    C:\Documents and Settings\Пользователь>
    

    This still won’t work for builtin modules. Again, you’ll need to invent some notion of “main code unit” for them.

    Update:

    I’ve looked through the internals called from _get_module_details and can say with confidence that they don’t even attempt to retrieve a code object from a module of type other than imp.PY_SOURCE, imp.PY_COMPILED or imp.PKG_DIRECTORY . So you have to patch this machinery this way or another for -m to work. Python fails before retrieving anything from your module (it doesn’t even check if the dll is a valid module) so you can’t do anything by building it in a special way.

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

Sidebar

Related Questions

I have a pure C++ application developed using VC 6.0. I would like this
I have written a Python module, and I have two versions: a pure Python
I'm using boost.python to make python-modules written in c++. I have some base class
I'd like to have a little php module. This module would have methods that
I have a pure Actionscript 3 project, using flash.* libraries, compiles down to about
Using pure functional languages can have a lot of benefits over using impure imperatives,
I have several modules (mainly C) that need to be redesigned (using C++). Currently,
I have been using the PHP mcrypt module for encrypting sensitive data at my
I have a project in pure Python with a rudimentary plugin system: you write
I have a simple, pure python project that I want to make available with

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.