【问题标题】:Error importing NumPy when executing an executable created with Py2exe执行使用 Py2exe 创建的可执行文件时导入 NumPy 时出错
【发布时间】:2019-07-14 11:31:03
【问题描述】:

我在 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,为什么不呢?我必须在安装文件中指定它吗?

【问题讨论】:

标签: python numpy executable py2exe


【解决方案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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2016-12-13
    相关资源
    最近更新 更多