Python模块导入问题

时间:2012-02-12 17:24:06

标签: python

我不会发布代码,我只会引用相关部分,因为它有点长。

所以,我有一个主.py文件,我定义了一些类。当我运行该文件并使用类中定义的过程时,一切正常。现在,我想创建一个主.py文件,它将导入包含所有定义的文件,即“from some_file import *”。现在,当我执行此文件时,一个过程不起作用并给出NameError(未定义全局名称some_attribute)。现在,此过程在其中一个类中定义,并使用在另一个类中创建的属性。出于某种原因,这个引用不起作用,我想知道我做错了什么。在这里,我将给出“main”文件中的代码:

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是我的类Node和Element的文件。 nd和el当然是他们的实例。 nodegen和elgen是方法。现在,另一个文件中的elgen方法引用了在调用nodegen过程之后创建的属性。而且我得到一个名称错误。即,elgen方法使用nd.nodes,这是一个由nodegen方法创建的列表。

我希望这很清楚,如有必要,我可以发布其他文件的代码。提前感谢您的帮助。

好的,这是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)

我把#nd引用放在Element类里面似乎有问题。对不起,代码有点长......(出现#nd引用注释的行是NameError行)

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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文件。