Python pyinstaller 创建 exe 后加载文件

1 投票
1 回答
882 浏览
提问于 2025-04-17 20:00

我现在在用Python 2.7,并且使用pyinstaller这个工具。我的目标是生成一个exe文件,并且让它能够运行我其他的类文件。我还在用https://code.google.com/p/dragonfly/这个框架来进行语音识别。我在dragonfly的示例目录下创建了一个新的文件,路径是dragonfly->examples->text.py。如果我在我的IDE中运行https://code.google.com/p/dragonfly/source/browse/trunk/dragonfly/examples/dragonfly-main.py?spec=svn79&r=79,我可以通过语音命令来控制,它会理解我创建的下面这个文件以及dragonfly示例中的其他文件。

    from dragonfly.all import Grammar, CompoundRule, Text, Dictation
import sys
sys.path.append('action.py')
import action

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    print "This works"
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print "Voice command spoken."

class AnotherRule(CompoundRule):
    spec = "Hi there"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print "Well, hello"




# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.add_rule(AnotherRule())                     # Add the command rule to the grammar.
grammar.load()           

                       # Load the grammar.

我在控制台中注意到,它会输出

UNKNOWN: valid paths: ['C:\\Users\\user\\workspace\\dragonfly\\dragonfly-0.6.5\\dragonfly\\examples\\action.py',etc..etc...

但是在我使用pyinstaller之后,那一行的输出变成了

UNKNOWN: valid paths: []

所以它没有加载示例文件,因为找不到它们。我该如何告诉pyinstaller在创建exe文件时也加载这些示例文件?如果它确实加载了这些文件,我又该如何确保我的exe知道这些文件在哪里呢?

我用来运行pyinstaller的命令是

C:\Python27\pyinstaller-2.0>python pyinstaller.py -p-paths="C:\Users\user\worksp
ace\dragonfly\dragonfly-0.6.5\dragonfly\examples\test.py" "C:\Users\user\workspa
ce\dragonfly\dragonfly-0.6.5\dragonfly\examples\dragonfly-main.py"

1 个回答

0

如果我理解得没错,你有一个自己的脚本,还有一些示例脚本是用来调用你的脚本,证明它能正常工作,对吧?

你似乎没有抓住重点。你的脚本应该是最终产品。

如果你想测试功能,就在开发版本中进行测试。
如果你想测试exe文件,就用另一个(独立的)测试脚本来做。

还有一点:
脚本和模块是完全不同的东西。
你现在是想把你的脚本当作模块来导入,并在示例脚本中使用它。

我建议你先建立一个主要的入口点,可以根据需要添加参数,这样做是正确的。然后再写一个示例脚本来运行你的脚本。

或者你可以先做一个模块,然后再写一个使用这个模块的脚本。接着把这个示例脚本打包成exe文件,展示它是如何工作的。

PyInstaller一次只能编译一个脚本,不需要强迫它去做一些不寻常的事情。

撰写回答