IronPython:使用pyc.py编译的EXE无法导入模块“os”

2024-05-16 04:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的IronPython脚本:

# Foo.py
import os

def main():
    print( "Hello" )

if "__main__" == __name__:
    main()

如果我用IronPython运行它,它会运行良好并打印Hello

ipy Foo.py

按照IronPython - how to compile exe中给出的说明,我使用以下命令将这个IronPython脚本编译为EXE:

ipy pyc.py /main:Foo.py /target:exe

执行Foo.exe会出现以下错误:

Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: No module named os
   at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
   at DLRCachedCode.__main__$1(CodeContext $globalContext, FunctionCode $functionCode)
   at IronPython.Compiler.OnDiskScriptCode.Run()
   at IronPython.Compiler.OnDiskScriptCode.Run(Scope scope)
   at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptC
ode, ModuleOptions options)

为什么找不到模块“os”?如何解决这个问题,以便我可以得到一个工作的EXE?

(请注意,这与问题IronPython cannot import module os不同,因为如果我使用ipy.exe运行脚本,则脚本可以正常工作。)


Tags: pyimport脚本hellocompilerfooosmain
2条回答

我知道这已经回答了很长一段时间,但它仍然花了我很多时间让我的exe工作。我指出了5条错误消息,其中有这个导入问题。这就是我要做的,使我的exe工作:

1)在部署文件夹(IronPython.dll、IronPython.Modules.dll等)中包含所有IronPython dll

2)在deploye文件夹中包含来自标准库的dll

3)exe所在的路径应该没有空间

(这: C: \User1\MyDocuments\My Folder\deployFolder

应更改为例如: C: \User1\MyDocuments\MyFolder\deployFolder)

4)您的操作系统可能会阻止exe文件使用的dll,并给您一个“loadfrommoteresource comutator error blablabla”。若要修复此问题,需要右键单击dll并在窗口的最后一行按“取消阻止”。

5)最后别忘了将pyc给出的dll文件包含在你的exe文件中(同名,只是扩展名不同)

这是我写的一篇文章,如果你需要的话,可以提供更多的细节:http://thesesergio.wordpress.com/2013/09/11/how-to-generate-and-use-a-exe-that-uses-net-dlls-with-ironpython-pyc-py/

构建一个可以分发的Ironpython EXE有点棘手,特别是在使用标准库的元素时。我的典型解决方案如下:

我将所需的所有stdlib模块复制到一个文件夹中(通常所有模块都只是为了完整起见),并使用此脚本构建我的exe。在本例中,我有两个文件FredMain.pyFredSOAP.py被编译成一个名为Fred_Download_Tool的EXE

import sys
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib')
sys.path.append(r'C:\Program Files\IronPython 2.7')
import clr

clr.AddReference('IronPython')
clr.AddReference('IronPython.Modules')
clr.AddReference('Microsoft.Scripting.Metadata')
clr.AddReference('Microsoft.Scripting')
clr.AddReference('Microsoft.Dynamic')
clr.AddReference('mscorlib')
clr.AddReference('System')
clr.AddReference('System.Data')

#
# adapted from os-path-walk-example-3.py

import os, glob
import fnmatch
import pyc

def doscopy(filename1):
    print filename1
    os.system ("copy %s .\\bin\Debug\%s" % (filename1, filename1))

class GlobDirectoryWalker:
    # a forward iterator that traverses a directory tree

    def __init__(self, directory, pattern="*"):
        self.stack = [directory]
        self.pattern = pattern
        self.files = []
        self.index = 0

    def __getitem__(self, index):
        while 1:
            try:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                # pop next directory from stack
                self.directory = self.stack.pop()
                self.files = os.listdir(self.directory)
                self.index = 0
            else:
                # got a filename
                fullname = os.path.join(self.directory, file)
                if os.path.isdir(fullname) and not os.path.islink(fullname) and fullname[-4:]<>'.svn':
                    self.stack.append(fullname)
                if fnmatch.fnmatch(file, self.pattern):
                    return fullname

#Build StdLib.DLL
gb = glob.glob(r".\Lib\*.py")
gb.append("/out:StdLib")    

#print ["/target:dll",]+gb

pyc.Main(["/target:dll"]+gb)

#Build EXE
gb=["/main:FredMain.py","FredSOAP.py","/target:exe","/out:Fred_Download_Tool"]
pyc.Main(gb)


#CopyFiles to Release Directory
doscopy("StdLib.dll")
doscopy("Fred_Download_Tool.exe")
doscopy("Fred_Download_.dll")


#Copy DLLs to Release Directory
fl = ["IronPython.dll","IronPython.Modules.dll","Microsoft.Dynamic.dll","Microsoft.Scripting.Debugging.dll","Microsoft.Scripting.dll","Microsoft.Scripting.ExtensionAttribute.dll","Microsoft.Scripting.Core.dll"]
for f in fl:

doscopy(f)

在我的脚本中,当我准备编译时,我添加了以下内容。这允许程序使用来自我的DLL的标准模块,而不是Python安装。如果要分发给未安装Python的用户,这是必要的。创建安装程序时,请确保包含必要的DLL。

#References to created DLL of python modules
clr.AddReference('StdLib')

相关问题 更多 >