属性错误:'module'对象没有'ascii_lowercase'属性。

3

我在编写一个程序时遇到了很大的麻烦,我决定将程序分成不同的文件,每个文件中有一个类,现在它给我以下错误提示:

File "principal.py", line 5, in <module>
import plotting.plot
File "/home/bojack/Dropbox/Maquina/obtencion/plotting/plot.py", line  1, in <module>
import matplotlib.pyplot as plt
File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 123,    in <module>
import pyparsing
File "/usr/local/lib/python2.7/dist-packages/pyparsing-2.0.3-py2.7.egg/pyparsing.py", line 160, in <module>
alphas = string.ascii_lowercase + string.ascii_uppercase
AttributeError: 'module' object has no attribute 'ascii_lowercase'

文件如下所示:

MyDir>>principal.py plot.py dxf.py linea.py string.py

如果我在其他目录中运行plot.py文件,它可以完美地工作,但是在该目录中无法正常工作,因此我将plot.py文件重新排列到子目录中,并添加了一个空的init.py文件,如Python Modules Contents所述。现在我没有更多的想法了。
以下是我的脚本代码:

dxf.py

#!/usr/bin/env python
class Lectura(object):
"""docstring for Lectura"""
    def __init__(self, archive):
        self.archive=archive
        self.usuario=getpass.getuser()


    def Pdf2Dxf(self):
        os.system("pstoedit -f 'dxf:-polyaslines -mm' /home/%s/Downloads/%s.pdf /home/%s/Dropbox/Maquina/dxfs/%s.dxf"%(self.usuario,self.archive,self.usuario,self.archive))        
        print "pstoedit -f 'dxf:-polyaslines -mm' /home/%s/Downloads/%s.pdf /home/%s/Dropbox/Maquina/dxfs/%s.dxf"%(self.usuario,self.archive,self.usuario,self.archive)
    def ai2dxf(self):
        os.system("pstoedit -f 'dxf:-polyaslines -mm' /home/%s/Downloads/%s.ai '/home/%s/Dropbox/Maquina/dxfs/%s.dxf'"%(self.usuario,self.archive,self.usuario,self.archive));

    def getDxf(self):   
        #La variable archive sera pedida en el programa principal
        archivo='/home/%s/Dropbox/Maquina/dxfs/%s.dxf' %(self.usuario,self.archive)
        dxf1 = dxfgrabber.readfile(archivo)
        return dxf1

linea.py

class Lineas(object):
    """docstring for Dibujo"""
    def __init__(self,dxf):
        self.dxf=dxf
        global start
        global end

    def grabLinea(self):

        start=[]
        end=[]
        for linea in self.dxf.entities:
            start.append(linea.start)
            end.append(linea.end)
            return (start,end)

    def vector(self,startC,endC):
        matrizX=[[0 for x in range(2)] for x in startC]
        matrizY=[[0 for x in range(2)] for x in startC]
        vector=[[0 for x in range(2)] for x in startC]
        longi=len(matrizX)
        for x in range(longi):
            matrizX[x][0]=startC[x][0]
            matrizX[x][1]=endC[x][0]

            matrizY[x][0]=startC[x][1]
            matrizY[x][1]=endC[x][1]

            vector[x][0]=matrizX[x][1]-matrizX[x][0]
            vector[x][1]=matrizY[x][1]-matrizY[x][0]
        return vector

string.py

#!/usr/bin/env python
class String(object):
    """docstring for String"""
    def __init__(self):
        pass

    def separar(self,matriz):
        matrizC = [[0 for x in range(3)] for x in matriz]  
        i=0
        j=0
        for n in matriz:
            n2=str(n)
            split=n2.split(',')
            for x in range(3):
                matrizC[i][x]=split[x]  
            i+=1    
        return matrizC

    def cortar(self,matriz):
        matrizC = [[0 for x in range(3)] for x in matriz]  
        i=0
        for linea in matriz:
            for coordenada in range(3):

                if coordenada==0:
                    corte=linea[0].strip()
                    matrizC[i][coordenada]=float(corte[1:])
                if coordenada==1:
                    corte=linea[1].strip()
                    matrizC[i][coordenada]=float(matriz[i][coordenada])

                if coordenada==2:

                    corte=linea[2].rstrip()
                    matrizC[i][coordenada]=0

            i+=1
        return matrizC  

plot.py

enter code here

import matplotlib.pyplot as plt
import numpy as np
class Graph(object):
    """docstring for Lectura"""
    def __init__(self, start):
        self.start=start

    def plot(self,start):
        start=np.array(start)
        longi=len(start)
        start=[]

        for i in range(longi):
            j=0
            start.append([start[i][j],end[i][j]])   

        maxx=np.max(start)
        minn=np.min(start)

        soa =np.array(start) 
        X,Y = zip(*soa)

        fig = plt.figure()
        ax = fig.add_subplot(111)
        print len(X)
        for i in range(len(X)-1):
            x_points = (X[i],X[i+1])
            y_points = (Y[i],Y[i+1])
            p = ax.plot(x_points, y_points, 'b')
            ax.set_xlabel('x-points')
            ax.set_ylabel('y-points')
            ax.set_title('Simple XY point plot')
            fig.show()
    def pltshow(self):
            plt.show()

principal.py

#!/usr/bin/env python
import dxf
import string
import linea
import plotting.plot
import matplotlib.pyplot
import numpy 
import dxfgrabber
import getpass
import os

class Principal(object):
    """docstring for Principal"""

    def programa(self):

        archivo=input('ingresa nombre del archivo : \n>>')
        #instaciamos objetos
        objdxf=dxf.Lectura(archivo)

        stringg=string.String()

        objdxf.Pdf2Dxf()
        linea1=objdxf.getDxf()
        lineas=linea.Lineas(linea1)
        start,end=lineas.grabLinea()
        startS=stringg.separar(start)
        endS=stringg.separar(end)
        startC=stringg.cortar(startS)
        endC=stringg.cortar(endS)

        plt=plot.Graph(startC,endC)

a=Principal()
a.programa()
1个回答

2
似乎您的文件夹中有一个名为string.py的文件,因此import string实际上将导入您的string.py,因为它具有与内置模块相同的名称,这将替换它。因此,我建议更改文件名,例如classes.py,并使用from classes import String
此外,您可以为主程序创建一个类,但这不是在Python中创建入口点的方式。
最好的做法是:
#!/usr/bin/env python
import dxf
import string
from classes import String
import linea
import plotting.plot
import matplotlib.pyplot
import numpy 
import dxfgrabber
import getpass
import os

def main():
    archivo=input('ingresa nombre del archivo : \n>>')
    #instaciamos objetos
    objdxf=dxf.Lectura(archivo)

    stringg=String()

    objdxf.Pdf2Dxf()
    linea1=objdxf.getDxf()
    lineas=linea.Lineas(linea1)
    start,end=lineas.grabLinea()
    startS=stringg.separar(start)
    endS=stringg.separar(end)
    startC=stringg.cortar(startS)
    endC=stringg.cortar(endS)

    plt=plot.Graph(startC,endC)

if __name__ == '__main__':
    main()

注意,只有在需要的情况下才应该导入模块,例如在主代码中使用 import string 但实际上没有使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接