tags:

views:

242

answers:

5
+2  A: 

why are you importing from XWinInfos? you should be importing from SomeUtils. Not to mention that *-style imports are discouraged.

Edit: your error

ImportError: cannot import name MyClass1

basically tells you that there is no MyClass1 defined in the SomeUtils. It could be because you have another SomeUtils.py file somewhere on the system path and it being imported instead. If that file doesn't have MyClass1, you'd get this error.

Again: it's irrelevant whether you class MyClass1 exist. What might be the case is that you have another XWinInfos.p(y|o|w) somewhere on your system and it's being imported. Otherwise: norepro.

SilentGhost
Thanks for posting. XWinInfos is a typing mistake so it is supposed to be SomeUtils. Still the problem persist with the same error message.
NawaMan
After your edit: Thanks again. I am sure that MyClass1 exist (copy-paste). And if it import another module, how can funct1 work.
NawaMan
I've post the actual code, please have a look ("EDIT 2").
NawaMan
+1  A: 

You may want to rewrite main.py as follows:

import SomeUtils as util

def main():
    util.funct1()               # Use funct1   without problem;
    aMyObj1 = util.MyClass1()   # Use MyClass1 with error

if __name__ == "__main__":
    main()

A few quick notes:

  • There is no need for semicolons in Python unless you have more than one statement on a line
  • There is no need to wrap conditional tests in parentheses except for grouping
  • from module import * is discouraged as it pollutes the global namespace
Triptych
Sorry about the typing mistake, I change the name so that it would distract the actual problem (but it seems to be a mistake here). Anyway, When I change the import section to: "from SomeUtils import funct1" and "from SomeUtils import MyClass1". I have this error "ImportError: cannot import name MyClass1".
NawaMan
I've post the actual code, please have a look ("EDIT 2").
NawaMan
+1  A: 

I suppose you mean

from SomeUtils import *

however, that does not trigger the error for me. This works fine for me:

SomeUtils.py

def funct1():
    print 4

class MyClass1:
    def __init__(self):
        print 8

main.py

from SomeUtils import *

def main():
    funct1()               # Use funct1   without problem;
    aMyObj1 = MyClass1()   # Use MyClass1 without error

if (__name__ == "__main__"):
    main()
BlackShift
You are right about my mis-type, but I did have problem with the Class.
NawaMan
I've post the actual code, please have a look ("EDIT 2").
NawaMan
+3  A: 

Hmm... there's several typos in your example, so I wonder if your actual code has some typos as well. Here's the complete source from a quick test that does work fine without import errors.

SomeUtils.py:

def funct1():
    print('Function 1')

def funct2():
    print('Function 2')

class MyClass1(object):
    def __init__(self):
        print('MyClass')

main.py:

from SomeUtils import *

def main():
    funct1()
    aObj = MyClass1()

if (__name__ == "__main__"):
    main()

[EDIT Based on OP additional info]

I still can't recreate the same error, but the code you posted won't initially work for at least a couple of errors in the XWinInfox.py init method:

self.WinID = pWinID #change to 'aWinID' since pWinID is not defined
self.Title = GetWindowTitle(pWinID)  #change to 'aWinID'since pWinID is not defined

so a corrected version would read:

self.WinID = aWinID
self.Title = GetWindowTitle(aWinID)

Also, you have a typo in your init file name, there should be two underscores before AND after the 'init' word. Right now you have '__init_.py' and it should be '__init__.py', however this shouldn't keep your code from working.

Because I don't have the RegExUtils.py code, I just stubbed out the methods that rely on that file. With the stubbed methods and correcting the aforementioned typos, the code you post now works.

gbc
I've post the actual code, please have a look ("EDIT 2").
NawaMan
OK, I updated my answer based on "EDIT 2".
gbc
A: 

Your question is naturally linked to a lot of SO older one. See, just for reference, SO1342128 and SO1057843

DrFalk3n
completely and utterly irrelevant to this question
SilentGhost