我编写了一个Python 3.6程序,它读取UTM坐标的.txt文件,按逆时针顺序排序,使用matplotlib在图形上显示坐标,然后将坐标写入桌面上的.txt文件。当我在spyder中运行它时工作正常,这是我一直使用的IDE,但是当我使用cx_Freeze(通过构建我的python文件)将其转换为exe并尝试运行它时,我收到以下错误:ModuleNotFoundError :没有名为'matplotlib.backends.backend_qt5agg'的模块我试过安装Qt5后端:pip install PyQt5以及更新cx_Freeze。任何帮助将非常感激。我仍然是Python的初学者以及一般的编程,所以如果我在解释中不够清楚,我会道歉。下面是我的主要Python脚本的代码
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import math as math
import tkinter as tk
from tkinter.filedialog import askopenfilename
import time
def findFilename():
root = tk.Tk()
#root.withdraw()
filename = askopenfilename()
root.destroy()
return(filename)
def findSize(cnt, filename):
#number of lines in file
with open(filename) as f:
for line in f:
cnt = cnt+1;
return cnt;
def findChar(filename):
file = open(filename, "r")
char = ""
lines = file.read()
if(lines.find(" ") == -1):
char = ","
else:
char = " "
return(char)
def inputArr(cnt, arrX, arrY, arrZ, filename, char):
file = open(filename, "r")
num1 = 0
num2 = 0
num3 = 0
place1 = 0
place2 = 0
place3 = 0
for i in range(0, cnt):
num = file.readline()
place1 = num.find(char)
num1 = num[:place1]
num = num[place1 + 1:]
place2 = num.find(char)
num2 = num[:place2]
num = num[place2+1:]
place3 = num.find(char)
num3 = num[:place3]
arrX.append(float(num1))
arrY.append(float(num2))
arrZ.append(float(num3))
return(arrX, arrY, arrZ)
def drawPath(arrX, arrY, label, cnt):
plt.plot(arrX, arrY, '-ok', color = 'red')
plt.xlabel("X coordinates")
plt.ylabel("Y coordinates")
plt.title("Loop path")
plt.show()
for i in range(0,cnt):
label.append(str(i))
for i, txt in enumerate(label):
plt.annotate(txt, (arrX[i], arrY[i]))
def findCenter(arrX, arrY, cnt):
xCenter = 0
yCenter = 0
for i in range(0,cnt):
xCenter += arrX[i]
yCenter += arrY[i]
xCenter /= cnt
yCenter /= cnt
return(xCenter, yCenter)
def moveToCenter(arrX, arrY, arrX1, arrY1, xCenter, yCenter, cnt):
for i in range(0,cnt):
arrX1.append(arrX[i] - xCenter)
arrY1.append(arrY[i] - yCenter)
return(arrX1, arrY1)
def calculateTheta(arrX1, arrY1, arrTheta, cnt):
for i in range(0,cnt):
arrTheta.append(math.atan2(arrY1[i], arrX1[i]))
# print(arrTheta[0])
return(arrTheta)
def sortPoints(arrTheta, arrX, arrY, arrZ, cnt):
minimum = 0
for i in range(0,cnt-1):
minimum = i
for j in range(i + 1, cnt):
if(arrTheta[j] < arrTheta[minimum]):
minimum = j
arrTheta[minimum], arrTheta[i] = arrTheta[i], arrTheta[minimum]
arrX[minimum], arrX[i] = arrX[i], arrX[minimum]
arrY[minimum], arrY[i] = arrY[i], arrY[minimum]
arrZ[minimum], arrZ[i] = arrZ[i], arrZ[minimum]
def writeFile(arrX, arrY, arrZ, cnt, char):
moment = time.strftime("%Y-%b-%d__%H_%M_%S",time.localtime())
file = open("C:\\Users\\natha\\Desktop\\sorted" + str(moment) + ".txt", "w")
num = ""
for i in range(0,cnt):
if(i < 10):
num = "0"
else:
num = ""
file.write("<" + "L" + num + str(i) + ">" + " " + str(arrX[i]) + char +
str(arrY[i]) + char + str(arrZ[i]) + char + "\n")
def main():
cnt = 0
arrX = []
arrY = []
arrZ = []
label = []
arrX1 = []
arrY1 = []
arrTheta = []
xCenter = 0
yCenter = 0
char = ""
filename = findFilename()
char = findChar(filename)
cnt = findSize(cnt, filename)
findChar(filename)
inputArr(cnt, arrX, arrY, arrZ, filename, char)
xCenter, yCenter = findCenter(arrX, arrY, cnt)
arrX1, arrY1 = moveToCenter(arrX, arrY, arrX1, arrY1, xCenter, yCenter, cnt)
arrTheta = calculateTheta(arrX1, arrY1, arrTheta, cnt)
#arrX, arrY, arrZ = randomPoints(arrX, arrY, arrZ, cnt)
sortPoints(arrTheta, arrX, arrY, arrZ, cnt)
writeFile(arrX, arrY, arrZ, cnt, char)
drawPath(arrX, arrY, label, cnt)
main()
和我的setup.py文件一样
from cx_Freeze import setup, Executable
import sys
import os.path
os.environ['TCL_LIBRARY'] = r'C:\Users\natha\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\natha\Anaconda3\tcl\tk8.6'
additional_mods = ['numpy.core._methods', 'numpy.lib.format']
setup(name='loopProgram',
version='0.4',
description='xyz script',
options = {'build_exe': {'includes': additional_mods}},
executables = [Executable('loopProgram.py')]
)
安装缺少的matplotlib qt5后端sudo apt-get install python-matplotlib-qt5
获取Matplotlib配置文件路径:python import matplotlib matplotlib.matplotlib_fname()u'/ usr / lib64 / python3.6 / site-packages / matplotlib / mpl-data / matplotlibrc'在配置文件中,将后端更改为qt5agg vi / USR / lib64下/ python3.6 /站点包/ matplotlib / MPL-数据/ matplotlibrc
将行更改为:
后端:qt5agg
您是否尝试添加以下行
import matplotlib.backends.backend_qt5agg
在您的主Python脚本(或脚本中使用的任何其他适当的模块)中“强制”cx_Freeze包含此模块?
对于OpenSUSE,我发现上述答案无效。我尝试了所有各种后端,没有任何显示。我没有从脚本运行中获得错误输出,而且plot.show()函数甚至没有阻止控制台。如plot.savefig()
所述,this thread在使用得当时没有问题
This link表示我需要在安装tk-devel
之前添加matplotlib
。使用Zypper我安装了tk-devel
,卸载了python3-matplotlib
,重新安装了matplotlib,这对我有用。