0

[Closing NOTE] Thank you everyone that trying to help me.

I've found the problem and it have nothing to do with python understanding of mine (which is little). :p

The problem is that I edit the wrong branch of the same project, Main.py in one branch and XWinInfos.py in another branch.

Thanks anyway.

[Original Question] I am a Java/PHP/Delphi programmer and only use Python when hack someone else program -- never to write a complex Python myself. Since I have a short free time this week, I determine to write something non-trivia with Python and here is my problem

First I have python files like this:

src/
    main.py
    SomeUtils.py

In "SomeUtils.py, I have a few functions and one class:

...
def funct1 ...

def funct2 ...

class MyClass1:
    __init__(self):
        self. ....
...

Then in "main.py", I use the function and class:

from SomeUtils import *;

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

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

The problem is that the functions are used without any problem what so ever but I cannot use the class.

The error is:

NameError: global name 'MyClass1' is not defined

What is the problem here? and What can I do?

EDIT: Thanks for answers for I still have problem. :( When I change the import statements to:

from SomeUtils import funct1
from SomeUtils import MyClass1

I have this error

ImportError: cannot import name MyClass1

EDIT 2:----------------------------------------------------------

Thanks you guys.

I think, it may be better to post the actual code, so here it is:

NOTE: I am aware about ";" and "(...)" but I like it this way.

Here is the dir structure. DIRS http://dl.getdropbox.com/u/1961549/images/Python_import_prolem_dir_.png as you see, I just add an empty init.py but it seems to make no different.

Here is main.py:


from XWinInfos import GetCurrentWindowTitle;
from XWinInfos import XWinInfo;

def main():
    print GetCurrentWindowTitle();
    aXWinInfo = XWinInfo();

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

Here is XWinInfos.py:

from subprocess import Popen;
from subprocess import PIPE;
from RegExUtils import GetTail_ofLine_withPrefix;

def GetCurrentWindowID():
    aXProp = Popen(["xprop", "-root"], stdout=PIPE).communicate()[0];
    aLine  = GetTail_ofLine_withPrefix("_NET_ACTIVE_WINDOW\(WINDOW\): window id # 0x", aXProp);
    return aLine;

def GetCurrentWindowTitle():
    aWinID    = GetCurrentWindowID(); 
    aWinTitle = GetWindowTitle(aWinID);
    return aWinTitle;

def GetWindowTitle(pWinID):
    if (aWinID == None): return None

    aWMCtrlList = Popen(["wmctrl", "-l"], stdout=PIPE).communicate()[0]; 
    aWinTitle   = GetTail_ofLine_withPrefix("0x[0-9a-fA-F]*" + aWinID + "[ ]+[\-]?[0-9]+[ ]+[^\ ]+[ ]+", aWMCtrlList);
    return aWinTitle;

class XWinInfo:
    def __init__(self):
        aWinID = GetCurrentWindowID();
        self.WinID = pWinID;
        self.Title = GetWindowTitle(pWinID);

The file RegExUtils.py holds a function "GetTail_ofLine_withPrefix" which work fine so.

If I use "from XWinInfos import *;", the error goes "NameError: global name 'XWinInfo' is not defined".

If I use "from XWinInfos import XWinInfo;", the error goes "ImportError: cannot import name XWinInfo".

Please helps. Thanks in advance.

NawaMan
  • 901
  • 5
  • 12

5 Answers5

3

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
  • 8,455
  • 6
  • 35
  • 30
2

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
  • 307,395
  • 66
  • 306
  • 293
  • 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 Sep 15 '09 at 15:45
  • After your edit: Thanks again. I am sure that MyClass1 exist (copy-paste). And if it import another module, how can funct1 work. – NawaMan Sep 15 '09 at 16:05
1

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
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
  • 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 Sep 15 '09 at 15:52
1

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
  • 2,296
  • 2
  • 19
  • 27
0

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

Community
  • 1
  • 1
DrFalk3n
  • 4,926
  • 6
  • 31
  • 34