cx_Freeze和networkx的问题

时间:2015-09-06 10:51:10

标签: python cx-freeze

我在Windows上使用64位python 2.7。当我用cx_Freeze构建一个可执行文件然后运行它时,我得到一个错误窗口(,替换实名):

---------------------------
cx_Freeze: Python error in main script
---------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "ui.py", line 8, in <module>
  File "C:\Users\<dir_name>\<file_name>.py", line 3, in <module>
    import networkx as nx
  File "C:\Python27\lib\site-packages\networkx\__init__.py", line 87, in <module>
    import networkx.readwrite
  File "C:\Python27\lib\site-packages\networkx\readwrite\__init__.py", line 14, in <module>
    from networkx.readwrite.gml import *
  File "C:\Python27\lib\site-packages\networkx\readwrite\gml.py", line 46, in <module>
    from lib2to3.refactor import RefactoringTool
  File "C:\Python27\lib\lib2to3\refactor.py", line 27, in <module>
    from .fixer_util import find_root
  File "C:\Python27\lib\lib2to3\fixer_util.py", line 9, in <module>
    from .pygram import python_symbols as syms
  File "C:\Python27\lib\lib2to3\pygram.py", line 32, in <module>
    python_grammar = driver.load_grammar(_GRAMMAR_FILE)
  File "C:\Python27\lib\lib2to3\pgen2\driver.py", line 121, in load_grammar
    g = pgen.generate_grammar(gt)
  File "C:\Python27\lib\lib2to3\pgen2\pgen.py", line 385, in generate_grammar
    p = ParserGenerator(filename)
  File "C:\Python27\lib\lib2to3\pgen2\pgen.py", line 15, in __init__
    stream = open(filename)
IOError: [Errno 2] No such file or directory: 'C:\\Users\\<dir_name>\\build\\exe.win-amd64-2.7\\library.zip\\lib2to3\\Grammar.txt'

---------------------------
OK   
---------------------------

该文件不在存档中,但如果我在构建后手动添加它,则仍然找不到该文件。看起来像路径(反斜杠?)问题。

有人知道如何解决这个问题吗?

另外:你认为使用pyinstaller可以更好地解决这类问题吗?我选择cx_Freeze的原因是pyinstaller本身的安装看起来非常重要,而cx_Freeze只是一个单独的。

2 个答案:

答案 0 :(得分:6)

我在使用Py2exe时遇到了同样的问题。

我发现问题是由NetworkX版本1.10引起的

回滚到1.9.1对我有用:

pip uninstall networkx
pip install networkx==1.9.1

答案 1 :(得分:4)

我不得不面对同样的问题,但无法回滚到networkx 1.9.1。

我通过从设置中排除lib2to3并使用&#34; includefiles&#34;将其包含回来解决了这个问题。 :

# -*- coding: cp1252 -*-

import myproj
from os.path import dirname
from cx_Freeze import setup, Executable

gui=Executable(
     script="myproj/gui.py",
     base="Win32Gui",
     targetName="myproj.exe",
     icon="logo.ico",
     )

packages=[]
includefiles=[]
includes=[]
excludes=['tkinter', 'lib2to3']

import lib2to3
lib23_path = dirname(lib2to3.__file__)
includefiles.append(lib23_path)

setup(
      name = 'myproj',
      version = myproj.version,
      author='myself',
      license='...',

      options = {'build_exe': {
                               'packages':packages,
                               'include_files':includefiles,
                               'includes':includes,
                               'excludes':excludes,
                               }},
      executables = [gui],

      )