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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:04:45+00:00 2026-05-27T07:04:45+00:00

I’m writing a python module in C, and looking for a way to write

  • 0

I’m writing a python module in C, and looking for a way to write a module inside of a module.

PyMODINIT_FUNC
initA(void)
{
PyObject*  pMod, pSubMod;
pMod = Py_InitModule3("A", A_Methods, A_Doc);
pSubMod = PyModule_New("B");
PyModule_AddStringConstant(pSubMod, "__doc__", B_Doc);
PyModule_AddIntConstant(pSubMod, "SOMETHING", 10);
PyModule_AddObject(pMod, "B", pSubMod);
... and so on ...

After compiling, I’m trying to access the module and its constant via various import methods

>>> import A
>>> print A.B.SOMETHING
10
>>> from A import B
>>> print B.SOMETHING
10
>>> from A.B import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named B

First two sound reasonable, and work fine. However, the last one doesn’t work. I’m expecting that I should have a code that’s equivalent to __init__.py script. However, I don’t want to write a separate .py file; rather I want to have such code in C init function directly.

For reference, I’m attaching __dict__ and __all__ of both modules.

>>> A.__dict__
{'B': <module 'B' (built-in)>, '__package__': None, '__file__': 'A.so'}
>>> A.B.__dict__
{'SOMETHING': 10, '__package__': None, '__name__': 'B', '__doc__': 'B_Doc'}
>>> A.__all__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__all__'
>>> A.B.__all__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__all__'

Thanks,

  • 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-27T07:04:45+00:00Added an answer on May 27, 2026 at 7:04 am

    You haven’t actually created an importable module called A.B, since A is not a package. You can’t create packages in C, only modules; packages are defined by the filesystem (or by an alternate loader, but that would not be pertinent here).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.