2012-02-12 193 views
1

我不會發布代碼,我只會引用相關部分,因爲它有點長。Python模塊導入問題

所以,我有一個主要的.py文件,我定義了一些類。當我運行該文件並使用類中定義的過程時,一切正常。現在,我想創建一個主要的.py文件,它將導入帶有所有定義的文件,即「from some_file import *」。現在,當我執行這個文件時,一個過程不起作用,並給出NameError(全局名稱some_attribute未定義)。現在,這個過程在其中一個類中定義,並使用另一個類中創建的屬性。出於某種原因,這個參考不起作用,我不知道我做錯了什麼。在這裏,我會從「主」文件給出代碼:

from fem_obj import * 
nd = Node() 
nd.nodegen(1, 1, 0, 0, 4, 0, 4) 
el = Element() 
el.elgen(1, 1, 1, 2, 1, 1, 3) 

fem_obj是我的課節點和單元文件。 nd和el當然是他們的實例。 nodegen和elgen是方法。現在,其他文件中的elgen方法引用了在調用nodegen過程之後創建的屬性。在那裏我得到一個名稱錯誤。即,elgen方法使用nd.nodes,它是由nodegen方法創建的列表。

我希望這已經足夠清楚了,如果需要的話我可以發佈其他文件的代碼。預先感謝您的幫助。

OK,這裏的代碼fem_obj:

from math import hypot, sqrt 
from Tkinter import * 

class Node: 

    def __init__(self): 
     self.nodes = [] 
     self.nodenum = 0 

    def nodegen(self, slabel, dlabel, sx, sy, dx, dy, num):  
     for i in range(1, num + 1):     
      label = slabel + (i - 1)*dlabel 
      if self.nodecheck(label) == True: 
       return "Node %s already exists!" %label    
      self.nodes = self.nodes + [[label, (sx + (i - 1)*dx, sy + (i - 1)*dy), (0, 0, 0)]] 
      self.nodenum += 1 

    def nodegenf(self, slabel, dlabel, sx, dx, num): 
     f = lambda x: sqrt(1 - x**2) 
     for i in range(1, num + 1):     
      label = slabel + (i - 1)*dlabel 
      if self.nodecheck(label) == True: 
       return "Node %s already exists!" %label    
      self.nodes = self.nodes + [[label, (sx + (i - 1)*dx, f(sx + (i - 1)*dx)), (0, 0, 0)]] 
      self.nodenum += 1  

    def nodeadd(self, label, sx, sy): 
     if self.nodecheck(label) == True: 
      return "Node %s already exists!" %label 
     self.nodes = self.nodes + [[label, (sx, sy), (0, 0, 0)]] 
     self.nodenum += 1   

    def nodedel(self, label): 
     if self.nodecheck(label) == False: 
      return "Node %s does not exist!" %label 
     for i in el.elements: 
      if label in i[1]: 
       return "Node %s attached to element %s!" %(label, i[0]) 
     for i in self.nodes: 
      if label == i[0]: 
       self.nodes.remove(i) 
     self.nodenum -= 1     

    def nodecheck(self, label): 
     for i in self.nodes: 
      if label == i[0]: 
       return True 
     return False 

    def noderes(self, label, u, v, r): 
     if self.nodecheck(label) == False: 
      return "Node %s does not exist!" %label 
     for i in self.nodes: 
      if label == i[0]: 
       i[2] = (u, v, r)  

    def nodelist(self): 
     if self.nodes == []: 
      return "No nodes defined!" 
     print "\n" 
     print "NUMBER OF NODES: ", self.nodenum 
     print "\n" 
     print "NODE COORDINATES RESTRAINTS" 
     print "---- ----------- ----------" 
     print "\n" 
     for i in self.nodes: 
      print i[0], " ", i[1], "  ", i[2] 


class Element: 

    def __init__(self): 
     self.elements = [] 
     self.elnum = 0 

    def elgen(self, slabel, dlabel, snl, enl, sndl, endl, num): 
     for i in range(1, num + 1):     
      label = slabel + (i - 1)*dlabel 
      if self.elcheck(label) == True: 
       return "Element %s already exists!" %label 
      if self.elements != []: 
       for j in self.elements: 
        if j[1] == (snl + (i - 1)*sndl, enl + (i - 1)*endl): 
         return "Element %s endnodes already taken!" %label 
      hlplst = [] 
      for j in nd.nodes: # nd reference 
       hlplst = hlplst + [j[0]] 
      if snl + (i - 1)*sndl not in hlplst: 
       return "Node %s does not exist" %(snl + (i - 1)*sndl) 
      if enl + (i - 1)*endl not in hlplst: 
       return "Node %s does not exist" %(enl + (i - 1)*endl)        
      self.elements = self.elements + [[label, (snl + (i - 1)*sndl, enl + (i - 1)*endl)]] 
      self.elnum +=1 
     self.ellen() 

    def elcheck(self, label): 
     for i in self.elements: 
      if label == i[0]: 
       return True 
     return False 

    def eladd(self, label, snl, enl): 
     if self.elcheck(label) == True: 
      return "Element %s already exists!" %label 
     if self.elements != []: 
      for j in self.elements: 
       if j[1] == (snl, enl):   
        return "Endnodes already taken by element %s!" %j[0] 
     hlplst = [] 
     for j in nd.nodes: # nd reference 
      hlplst = hlplst + [j[0]] 
     if snl not in hlplst: 
      return "Node %s does not exist" %snl 
     if enl not in hlplst: 
      return "Node %s does not exist" %enl 
     self.elements = self.elements + [[label, (snl, enl)]] 
     self.elnum +=1 
     self.ellen() 

    def eldel(self, label): 
     if self.elcheck(label) == False: 
      return "Element %s does not exist!" %label 
     for i in self.elements: 
      if label == i[0]: 
       self.elements.remove(i) 
     self.elnum -=1 

    def ellen(self): 
     if self.elements == []: 
      return "No elements defined!" 
     for i in self.elements: 
      if len(i) == 2: 
       x1 = y1 = x2 = y2 = 0 
       for j in nd.nodes: 
        if i[1][0] == j[0]: 
         x1 = j[1][0] 
         y1 = j[1][1] 
        elif i[1][1] == j[0]: 
         x2 = j[1][0] 
         y2 = j[1][1] 
       i.append(round(hypot(x1 - x2, y1 - y2), 4)) 
      else: 
       continue    

    def ellist(self): 
     if self.elements == []: 
      return "No elements defined!" 
     print "\n" 
     print "NUMBER OF ELEMENTS: ", self.elnum 
     print "\n" 
     print "ELEMENT START NODE END NODE LENGTH SECTION" 
     print "------- ---------- -------- ------ -------" 
     print "\n" 
     for i in self.elements: 
      if len(i) < 4: 
       print i[0], "   ", i[1][0], "   ", i[1][1], "  ", i[2], "  ", "NONE" 
      else: 
       print i[0], "   ", i[1][0], "   ", i[1][1], "  ", i[2], "  ", i[3]     

    def sctassign(self, label, slabel, elabel, dlabel): 
     for i in s1.sections: # s1 reference 
      if label == i[0]: 
       break 
      return "Section %s does not exist!" %label 

     j = 0 
     while slabel + j*dlabel <= elabel: 
      for i in self.elements: 
       if i[0] == slabel + j*dlabel: 
        i.append(label) 
        j += 1 
        continue 
       else: 
        return "Element %e does not exist!" %(slabel + j*dlabel) 

我把那裏似乎是一個問題的Element類內部#nd參考。對不起,代碼有點長...(#nd引用註釋出現的那行是NameError行)

+0

我沒有得到elgen和nd.nodes之間的連接。 el.elgen如何知道甚至存在,訪問nd.nodes?有一些隱藏的全球狀態嗎? – DSM 2012-02-12 17:28:29

+0

其他文件的代碼是相關的,因爲它聽起來就像是生成NameError的地方(雖然它有點不清楚)。btw,'從foo import *'是一個可怕的想法。 – geoffspear 2012-02-12 17:29:11

+0

請發佈其他文件的代碼。這是必要的,以避免猜測:) – 2012-02-12 17:29:21

回答

3

也許我誤解了,因爲這看起來很直接。名稱錯誤是:

Traceback (most recent call last): 
    File "main.py", line 5, in <module> 
    el.elgen(1, 1, 1, 2, 1, 1, 3) 
    File "/home/snim2/Dropbox/scratch/so/fem_obj.py", line 86, in elgen 
    for j in nd.nodes: # nd reference 
NameError: global name 'nd' is not defined 

和線86只出現在此背景下:

def elgen(self, slabel, dlabel, snl, enl, sndl, endl, num): 
    for i in range(1, num + 1):     
     label = slabel + (i - 1)*dlabel 
     if self.elcheck(label) == True: 
      return "Element %s already exists!" %label 
     if self.elements != []: 
      for j in self.elements: 
       if j[1] == (snl + (i - 1)*sndl, enl + (i - 1)*endl): 
        return "Element %s endnodes already taken!" %label 
     hlplst = [] 
     for j in nd.nodes: # nd reference 
      hlplst = hlplst + [j[0]] 
     if snl + (i - 1)*sndl not in hlplst: 
      return "Node %s does not exist" %(snl + (i - 1)*sndl) 
     if enl + (i - 1)*endl not in hlplst: 
      return "Node %s does not exist" %(enl + (i - 1)*endl)        
     self.elements = self.elements + [[label, (snl + (i - 1)*sndl, enl + (i - 1)*endl)]] 
     self.elnum +=1 
    self.ellen() 

所以,如果你想elgen能夠使用的Node是你的數組,你需要將它們傳遞到該方法作爲參數,如下所示:

def elgen(self, nd, slabel, dlabel, snl, enl, sndl, endl, num): 
    for i in range(1, num + 1):     
     label = slabel + (i - 1)*dlabel 
     if self.elcheck(label) == True: 
      return "Element %s already exists!" %label 
     if self.elements != []: 
      for j in self.elements: 
       if j[1] == (snl + (i - 1)*sndl, enl + (i - 1)*endl): 
        return "Element %s endnodes already taken!" %label 
     hlplst = [] 
     for j in nd.nodes: # nd reference 
      hlplst = hlplst + [j[0]] 
     if snl + (i - 1)*sndl not in hlplst: 
      return "Node %s does not exist" %(snl + (i - 1)*sndl) 
     if enl + (i - 1)*endl not in hlplst: 
      return "Node %s does not exist" %(enl + (i - 1)*endl)        
     self.elements = self.elements + [[label, (snl + (i - 1)*sndl, enl + (i - 1)*endl)]] 
     self.elnum +=1 
    self.ellen() 

和在main.py

nd = Node() 
nd.nodegen(1, 1, 0, 0, 4, 0, 4) 
el = Element() 
el.elgen(nd, 1, 1, 1, 2, 1, 1, 3) 
+0

我編輯了代碼,並將「nd」作爲參數添加到使用它的過程中。現在它似乎工作。謝謝您的幫助。 – 2012-02-12 22:29:56

0

我沒有去找出代碼應該做的所有事情,然而,你有困難的線...

for j in nd.nodes: # nd reference 

它引用一個變量「nd」。除了在main.py之外,我沒有看到「nd」在哪裏定義。 main.py不會被發現。您可以簡化這個問題分爲以下錯誤的代碼:

main.py:

from feb_obj import run 
nd = "Test" 
run() 

feb_obj.py:

def run(): 
    print nd # This won't work! 

我相信你期待它知道 「ND」是「main.py」文件中定義的變量。這不是python的工作原理。 「fem_obj.py」只會看到自己範圍內定義的內容。它知道「hypot」和「sqrt」是什麼,因爲你輸入了它們。但是,它不知道其他模塊中的變量,包括main.py文件。

+0

那麼,nd是在Nodes類中定義的,而在主模塊中,這是在我引用它的部分之前執行的。這對我來說還是有點困惑 - 我不確定這個變量是哪個命名空間 - 顯然是在fem_obj模塊中,因爲主模塊看不到它。 – 2012-02-12 22:31:37