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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:07:14+00:00 2026-06-14T10:07:14+00:00

Let’s consider the package structure as below: myApp |– myPackage | |– __init__.py |

  • 0

Let’s consider the package structure as below:

myApp
|-- myPackage
|    |-- __init__.py 
|    +-- myModule.py
|-- __init__.py
+-- main.py

myModule.py contains a single class such as:

class MyClass( object ):
    _myList = []

    @classmethod
    def test( cls ):
        cls._myList.append( len( cls._myList ) )
        return cls._myList

    def __init__( self ):
        return

    pass

As you can see, there’s nothing fancy, I’m just defining a list as a static class variable.
Now let’s consider the code from main.py:

from myApp.myPackage.myModule import MyClass as MyClassAbs
from myPackage.myModule import MyClass as MyClassRel

if __name__ == '__main__':

    print '\n  myList'
    print 'MyClassAbs().test():', MyClassAbs().test() #< Prints [0].
    print 'MyClassRel().test():', MyClassRel().test() #< Prints [0] but should be [0, 1].
    print 'MyClassAbs.test():', MyClassAbs.test() #< Prints [0, 1] but should be [0, 1, 2].
    print 'MyClassRel.test():', MyClassRel.test() #< Prints [0, 1] but should be [0, 1, 2, 3].

    print '\n  myList ids'
    print id( MyClassAbs().test() )
    print id( MyClassRel().test() )
    print id( MyClassAbs.test() )
    print id( MyClassRel.test() )

    print ''
    print 'MyClassAbs == MyClassRel:', MyClassAbs == MyClassRel #< Prints False
    print 'isinstance( MyClassAbs(), MyClassRel ):', isinstance( MyClassAbs(), MyClassRel ) #< Prints False
    print 'isinstance( MyClassRel(), MyClassAbs ):', isinstance( MyClassRel(), MyClassAbs ) #< Prints False

    pass

The issue is that in my code I’m importing twice the same class from the same module but in different ways: once as a an absolute import and once as a relative one. As seen in the last part of the code, even though the classes are the same, they are not equal because their module are being registered distinctively into the sys.modules:

'myApp.myPackage.myModule': <module 'myApp.myPackage.myModule' from '/full/path/myApp/myPackage/myModule.py'>
'myPackage.myModule': <module 'myPackage.myModule' from '/full/path/myApp/myPackage/myModule.py'>

and as a result, their representation differs:

'MyClassAbs': <class 'myApp.myPackage.myModule.MyClass'>
'MyClassRel': <class 'myPackage.myModule.MyClass'>

So… is there any way to set this variable as static for good?

Edit:
The code above is a obviously only a reduction of my real problem. In reality, I basically have a piece of code that inspects all the modules recursively nested within a folder and that registers the classes contained in there. All the classes registered this way can then be instanciated using a common syntax such as

myApp.create( 'myClass' )

This is why I sometimes end up having 2 objects pointing to the same class but that have been imported in different ways. One has been imported automatically through a imp.load_module() call, and the other would have been directly imported by the user through a conventional import statement. If the user decides to manually import a class, he should still have access to the same static class variable than the one defined within the same but automatically registered class. Hope it makes sense?

  • 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-14T10:07:16+00:00Added an answer on June 14, 2026 at 10:07 am

    No. (At least, not without very ugly and fragile hacks.) When you import it in those two different ways, the module is actually imported twice. Python usually re-uses an already-imported module if you import it again, but this is based not on the actual file imported, but on the path relative to sys.path. So if you import myModule.py once absolutely and once relatively, the whole file actually gets executed twice. You can see this if you do something like:

    from myApp.myPackage import myModule
    import myApp.myPackage.myModule as sameModule
    import myPackage.myModule
    print myModule is sameModule
    print myModule is myPackage.myModule
    

    You should see True for the first print (because the first two imports are via the same path) but False for the second (because the third import uses a different path). This is also why you’re seeing the module with two entries in sys.modules.

    Because the whole module is imported twice, you actually have two different classes called MyClass, not one. There’s no legitimate way to have those two classes share their state. It’s just as if you’d imported two different classes from two different modules. Even though they happen to be defined in the same source file, they’re not linked in any way when you double-import them like that. (There is an evil way to link them, which is that you can include code in your module to manually check if it’s been imported under a different name, and then manually mess with sys.modules to set the currently-importing module to the already-imported one. But this is a bad idea, because it can be hard to tell if what’s being imported is really the same module, and also because stomping on sys.modules can lead to strange bugs if either the original import or the new import failed for any reason.)

    The real question is, why are you importing the module twice? The second question is, why are you using relative imports at all? The solution is to just import the module once using absolute imports, and then you’ll have no problems.

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

Sidebar

Related Questions

Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
Let's say I have the following structure: abstract class Hand {} class Rock extends
Let's say you have a class called Customer, which contains the following fields: UserName
Let me explain best with an example. Say you have node class that can
Let's say I have a custom class CustomClass, and I have a collection deriving
Let's consider this code: (function(){ var a = {id: 1, name: mike, lastname: ross};
Let's say I have the following classes : public class MyProductCode { private String
Let assume we have two activities. A - main activity, that is home launcher
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.