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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:31:15+00:00 2026-06-15T08:31:15+00:00

The header might be quite hard to understand, but I’ll try to explain as

  • 0

The header might be quite hard to understand, but I’ll try to explain as well as I can.

I have a folder called SubModule which includes total of 3 files:

__init__.py
Class1.py
Class2.py

Inside __init__.py, I got this:

import Class1
import Class2

def main():
    c1 = Class1.Class1()
    c2 = Class2.Class2()

if __name__ == "__main__":
    main()

And it all works fine with no errors, and I can use c1 and c2 properly.


The problem appears, when I try to move SubModule folder under an other module’s (let’s call it MainModule) folder.
So I would have the following:

MainModule\__init__.py
MainModule\SubModule\__init__.py
MainModule\SubModule\Class1.py
MainModule\SubModule\Class2.py

Now when I try to import SubModule from MainModule\__init__.py like so:

import SubModule
c1 = SubModule.ClassOne.ClassOne()
c2 = SubModule.ClassTwo.ClassTwo()

I would expect this to work.
However, running the MainModule\__init__.py raises an error from SubModule\__init__.py:

Traceback (most recent call last):
  File "C:\...\MainModule\__init__.py", line 1, in <module>
    import SubModule
  File "C:\...\MainModule\SubModule\__init__.py", line 1, in <module>
    import Class1
ImportError: No module named 'Class1'

As you can see, the error comes from import Class1, and it tells me that there’s No module named 'Class1'.
This is cause the path is now SubModule.Class1, instead of only Class1.
I can get rid of this error by changing SubModule\__init__.py to this:

import SubModule.Class1
import SubModule.Class2

However, I can no longer use the SubModule alone by running SubModule\__init__.py, since the path would be import Class1 again, instead of import SubModule.Class1.

This is making me crazy, is there a way to generalize the importing, so it doesn’t matter which module imports SubModule?

  • 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-15T08:31:16+00:00Added an answer on June 15, 2026 at 8:31 am

    There’s a way, not exactly ‘general’, but …

    try:
        import Submodule.Class1 as Class1
    except ImportError:
        import Class1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound strange at first but its rather simple. I have a wcf
header(Content-type: image/gif); readfile($filename); The above can only be used to show gif images. Is
in header file I have defined the following function #ifndef OS_H #define OS_H #include
The task: I want to write a C++ header file that can load a
I think I might have greatly misunderstood the usage of gluperspective() and require help.
This might be a very simple question for you, but I'm stumped. I'm trying
I have a header file containing a lot of small inline functions. Most of
I have a Windows project that also works on XCode. But I had to
Maybe this sounds odd, but I have the feeling I am doing something wrong
I've got a django site running quite happily with django-cms but now I want

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.