簡體   English   中英

Cx_freeze ImportError 沒有名為 scipy 的模塊

[英]Cx_freeze ImportError no module named scipy

大家好,

我在正在轉換為 .exe 的代碼上使用 cx_Freeze 時遇到問題。

當我運行 cx_Freeze 時,我收到以下 ImportError,即沒有名為 scipy 的模塊

running install
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 25, in <module>
    executables = executables
  File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
    distutils.core.setup(**attrs)
  File "C:\Python34\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\distutils\command\install.py", line 539, in run
    self.run_command('build')
  File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\distutils\command\build.py", line 126, in run
    self.run_command(cmd_name)
  File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 232, in run
    freezer.Freeze()
  File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 619, in Freeze
    self.finder = self._GetModuleFinder()
  File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 378, in _GetModuleFinder
    finder.IncludePackage(name)
  File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 686, in IncludePackage
    module = self._ImportModule(name, deferredImports)
  File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 386, in _ImportModule
    raise ImportError("No module named %r" % name)
ImportError: No module named 'scipy'

我可以確認我的系統上安裝了 Scipy 0.16,當我將其導入其他 python 代碼時,它可以工作。 我目前在 Windows 上運行 python 3.4。 以下是我用於 cx_Freeze 的 setup.py 文件。

import cx_Freeze
import sys
import matplotlib

base = None

if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [cx_Freeze.Executable('fractureGUI.py', base=base, icon='star_square.ico')]

packages = ['tkinter','matplotlib','scipy']

include_files = ['star_square.ico', 'C:\\Python34\\Lib\\site-packages\\scipy']

cx_Freeze.setup(
    name = 'FracturePositionMonteCarlo',
    options = {'build_exe': {'packages':packages,
        'include_files':include_files}},
    version = '0.01',
    description = 'Fracture Depth Monte Carlo',
    executables = executables
    )

以下是我的主腳本fractureGUI.py 的導入部分。

import scipy
from random import random

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import style
style.use('ggplot')

import tkinter as tk
from tkinter import ttk, filedialog

import sys
import json

如果有人對 cx_Freeze 無法找到 scipy 的原因有任何想法,請告訴我。 我嘗試將文件路徑添加到 include_files 下的 scipy 中,但沒有任何區別。

親切的問候,

約翰尼什曼

我有完全相同的問題。 在這里找到解決方案: https : //bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with

在 cx_freeze 文件夾中找到 hooks.py 文件。 將第 548 行從 finder.IncludePackage("scipy.lib") 更改為 finder.IncludePackage("scipy._lib")。

保留包中的“scipy”條目並刪除 include_files 中的“C:\\Python34\\Lib\\site-packages\\scipy”。

對於所有與 Scipy 相關的問題,如果您將它們包含在腳本中,就會得到解決。 它對我有用。 請參考我的工作腳本(注意:這個腳本沒有任何像 tkinter 這樣的 UI 庫)

此腳本從配置文件中獲取數據並返回寫入工作目錄中文件的加法兩個數字。

文件夾結構

在此處輸入圖片說明

設置文件

import sys
import cx_Freeze
from cx_Freeze import setup, Executable
from scipy.sparse.csgraph import _validation
import scipy
import matplotlib

'''Include the package for which you are getting error'''
packages = ['matplotlib','scipy']
executables = [cx_Freeze.Executable('main.py', base='Win32GUI')]

'''include the file of the package from python/anaconda installation '''
include_files = ['C:\\ProgramData\\Continuum\\Anaconda\\Lib\\site-packages\\scipy']

cx_Freeze.setup(
    name = 'Test1',
    options = {'build_exe': {'packages':packages,
        'include_files':include_files}},
    version = '0.1',
    description = 'Extraction of data',
    executables = executables
    )

主文件

import os, numpy as np
import configparser
from helper_scripts.help1 import Class_A

path = os.path.dirname(os.path.abspath('__file__')) + '\\'
conf = configparser.ConfigParser()
conf.read(path + 'config.ini')

a = eval(conf.get('inputs','input_1'))
b = eval(conf.get('inputs','input_2'))

obj = Class_A()

res = obj.getData(a,b)

if not os.path.exists(path + 'Result.txt'):
    with open(path + 'Result.txt', 'w', encoding ='utf-8') as f:
        f.write(f'result is : {str(res)}\n')

else:
    with open(path + 'Result.txt', 'a', encoding ='utf-8') as f:
        f.write(f'result is : {str(res)}\n')

生成exe文件的命令

''' make sure  to run the below command from working directory where the setup.py file is present.'''

python setup.py build

build 文件夾使用 main.exe 文件創建,其中包含所有必需的二進制文件。

注意:將 config.ini 文件放在 exe 文件夾中,以便 exe 可以訪問配置文件並生成輸出。

在此處輸入圖片說明

不幸的是,我仍然沒有代表發表評論,但對於“沒有名為 scipy.spatial.ckdtree 的模塊”錯誤的問題,我通過簡單地將“cKDTree.cp37-win_amd64”重命名為“ckdtree.cp37-”來解決它win_amd64" 在 scipy\\spatial 文件夾下。 我在這里和那里用大寫字母導入庫時遇到了類似的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM