由Python的冻结工具

时间:2015-11-25 07:07:35

标签: python importerror

我在Linux Ubuntu中安装了python2.7.10。(我使用的是源代码tarball)

./configure
make

我想获得一个可执行的二进制文件,用于未安装Python的远程服务器。

我使用了命令

python freeze.py -o ./dist_time Test_time.py

'Test_time.py'的源代码:

import datetime

now = datetime.datetime.now()

print
print "Current date and time using str method of datetime object:"
print str(now)

print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond

print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")

我收到'Test_time'可执行二进制文件。

我将二进制文件(Test_time)移动到远程服务器(未安装Python)。

当我在那里执行'Test_time'binary文件时,我收到此错误消息:

./Test_time

Traceback (most recent call last):
  File "Test_time.py", line 3, in <module>
ImportError: No module named datetime

为什么datetime模块没有内置到可执行文件中?如何将模块包含在可执行文件中?

1 个答案:

答案 0 :(得分:0)

您可以使用cx_freeze从脚本创建可执行文件,它将生成包含可执行文件及其依赖项的目录。
您需要在同一目录中创建setup.py

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == "posix":
    base = "Linux"

setup(  name = "times",
        version = "0.1",
        description = "My demo application!",
        executables = [Executable("time.py", base=base)])

然后使用此命令python setup.py build运行它。

相关问题