本文介绍了AttributeError:“模块"对象没有属性"ascii_lowercase"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having big troubles coding a program, I decided to make the program on different files, having one class on each file and now it gives me the following error

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

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

如果我在其他目录上运行plot.py文件,则该文件可以完美运行,但不能在该目录下运行,因此我在一个子目录上重新放置了plot.py文件,并添加了一个 init .py空白文件也存储到子目录中 Python模块的内容现在,我没有主意了.

If I run the plot.py file on a different directory, it works perfectly, it just doesn't work on that directory, so I rearrenged the plot.py file on a subdirectory , adding an init.py blank file also to the subdirectory as stated on Python Modules Contents And now I'm out of ideas.

这是我的脚本代码

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()

推荐答案

似乎您的文件夹中有一个string.py文件,因此import string实际上将导入您的string.py,因为它的名称与一个内置模块,它将取代它.因此,我建议更改文件名,例如classes.py,而不要使用from classes import String

it seems you have a string.py file in your folder so the import string will in fact import your string.py because since it has the same name of a built-in module, that gonna replace it. So I recommend to change the name of your file like classes.py and rather use from classes import String

还要为您的主程序创建一个类,这不是在Python中创建入口点的方法.

Also you make a class for your main program, it's not the way to make an entry point in 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,但没有使用它.

Note also that you should only import when needed, for example you use import string in your main code but you don't use it.

这篇关于AttributeError:“模块"对象没有属性"ascii_lowercase"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:06