Python Freeze工具创建的独立可执行文件引发ImportError异常

3

我在Linux Ubuntu下安装了Python 2.7.10。(使用源代码压缩包安装)

./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'二进制文件时,出现了以下错误信息:

./Test_time

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

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

你在远程服务器上使用了 virtualenv 吗? - Ayush
你使用什么工具进行冻结? - utkbansal
1
@ Ayush Shanker 不,我只是将“time”文件从本地移动到远程服务器,并使用“./time”命令。 - J.Tae
1
datetime 模块在 Python 2.3 版本之后始终包含在标准库中。因此,如果您没有它,我建议您升级服务器上的 Linux 版本。 - Games Brainiac
@GamesBrainiac
我的服务器Linux版本是12.04。Python版本是2.7.10。 如果Python始终包含datetime模块,为什么找不到datetime模块?
- J.Tae
@moooeeeep,我复制了“time”可执行文件。 - J.Tae
1个回答

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接