ImportError by standalone executable created by Python's Freeze tool
Question: I have installed python2.7.10 in Linux Ubuntu.(I use source tarball)

    
    
    ./configure
    make
    

I want to get an executable binary file to use in a remote server that does
not have Python installed.

I used the command

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

source code of '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")
    

I receive 'Test_time' executable binary.

I moved the binary file(Test_time) to the remote server (where Python is not
installed).

When I execute 'Test_time'binary file there, I get this error message:

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

Why is the datetime module not built into the executable? How do I include the
module in the executable file?


Answer: You can use `cx_freeze` to create an executable from your script, it will
generate a directory containing an executable and its dependances.  
You need to create a `setup.py` in the same directory.

    
    
    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)])
    

Then run it with this command `python setup.py build`.

