Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with import in Python
    text
    copied!<p>[Closing NOTE] Thank you everyone that trying to help me.</p> <p>I've found the problem and it have nothing to do with python understanding of mine (which is little). :p</p> <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.</p> <p>Thanks anyway.</p> <p>[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</p> <p>First I have python files like this:</p> <pre><code>src/ main.py SomeUtils.py </code></pre> <p>In "SomeUtils.py, I have a few functions and one class:</p> <pre><code>... def funct1 ... def funct2 ... class MyClass1: __init__(self): self. .... ... </code></pre> <p>Then in "main.py", I use the function and class:</p> <pre><code>from SomeUtils import *; def main(): funct1(); # Use funct1 without problem; aMyObj1 = MyClass1(); # Use MyClass1 with error if (__name__ == "__main__"): main(); </code></pre> <p>The problem is that the functions are used without any problem what so ever but I cannot use the class.</p> <p>The error is:</p> <pre><code>NameError: global name 'MyClass1' is not defined </code></pre> <p>What is the problem here? and What can I do?</p> <p>EDIT: Thanks for answers for I still have problem. :( When I change the import statements to:</p> <pre><code>from SomeUtils import funct1 from SomeUtils import MyClass1 </code></pre> <p>I have this error </p> <pre><code>ImportError: cannot import name MyClass1</code></pre> <p><b>EDIT 2:----------------------------------------------------------</b></p> <p>Thanks you guys.</p> <p>I think, it may be better to post the actual code, so here it is:</p> <p>NOTE: I am aware about ";" and "(...)" but I like it this way.</p> <p>Here is the dir structure. <a href="http://dl.getdropbox.com/u/1961549/images/Python_import_prolem_dir_.png" rel="nofollow noreferrer">DIRS http://dl.getdropbox.com/u/1961549/images/Python_import_prolem_dir_.png</a> as you see, I just add an empty <strong>init</strong>.py but it seems to make no different.</p> <p>Here is main.py:</p> <pre><code> from XWinInfos import GetCurrentWindowTitle; from XWinInfos import XWinInfo; def main(): print GetCurrentWindowTitle(); aXWinInfo = XWinInfo(); if (__name__ == "__main__"): main(); </code></pre> <p>Here is XWinInfos.py:</p> <pre><code>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); </code></pre> <p>The file RegExUtils.py holds a function "GetTail_ofLine_withPrefix" which work fine so.</p> <p>If I use "from XWinInfos import *;", the error goes "NameError: global name 'XWinInfo' is not defined".</p> <p>If I use "from XWinInfos import XWinInfo;", the error goes "ImportError: cannot import name XWinInfo".</p> <p>Please helps. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload