执行使用Py2exe创建的可执行文件时导入NumPy时出错

时间:2019-02-20 17:22:04

标签: python numpy executable py2exe

我在Windows上使用Py2exe实现了我的第一个可执行文件。该脚本使用库:

import os
import pandas as pd
import numpy as np
from pandas import ExcelWriter
import datetime as dt

我的设置文件是:

from cx_Freeze import setup, Executable
import os
import sys

os.environ['TCL_LIBRARY'] = r'C:\Program Files\Continuum\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Continuum\Anaconda3\tcl\tk8.6'

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name = "my first executable",
    version = "0.1",
    description = "Executable",
    executables = [Executable("myscript.py")])

我试图测试我的。 exe,方法是从终端启动命令:

>> myscript.exe

但是会返回错误:

ImportError:缺少必需的依赖项['NumPy']。

如何解决此错误?我安装了NumPy,为什么不呢?我必须在安装文件中指定它吗?

1 个答案:

答案 0 :(得分:1)

如果您想尝试PyInstaller,请使用以下小脚本来简化生活:

import sys, os
import tkinter as tk
from tkinter import filedialog

print(
    """
=======================================
Create a .exe file from a Python Script
=======================================

Select the Python script you want to create the .exe from:

""")

root = tk.Tk()
root.withdraw()

file_p = filedialog.askopenfilename(initialdir = "./", title = "Select file", filetypes = ((".py files","*.py"), (".pyw files","*.pyw"))) 

if file_p == "." or file_p == None:
    sys.exit()

if file_p.endswith('.pyw'):
    cmd = ('pyinstaller.exe --windowed --onefile ' + '"' + file_p + '"')
    os.system(cmd)

if file_p.endswith('.py'):
    cmd = ('pyinstaller.exe --onefile ' + '"' + file_p + '"')
    os.system(cmd)

os.system('pause')

它在脚本所在位置附近的dist文件夹中创建一个.exe。