问题
I realized my first executable with Py2exe on Windows. The script uses libraries:
import os
import pandas as pd
import numpy as np
from pandas import ExcelWriter
import datetime as dt
My setup file is:
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")])
I tried to test my. exe by launching the command from the terminal:
>> myscript.exe
But the error is returned:
ImportError: Missing required dependencies [' NumPy '].
How can I fix this error? I installed NumPy, why not? Do I have to specify it in the setup file?
回答1:
If you want to give PyInstaller a try, I use this little script to make my life easier:
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')
It creates a single .exe in a dist folder located next to wherever the script is.
来源:https://stackoverflow.com/questions/54792050/error-importing-numpy-when-executing-an-executable-created-with-py2exe