AttributeError:'模块'对象没有属性' ascii_lowercase'

时间:2015-05-21 01:02:57

标签: python python-2.7 matplotlib module

我在编写程序时遇到了大麻烦,我决定在不同的文件上创建程序,每个文件都有一个类,现在它给了我以下错误

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'

文件在dir上如下

MyDir&gt;&gt; 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 个答案:

答案 0 :(得分:1)

您的文件夹中似乎有一个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但不使用它。