Search code examples
pythonpython-2.7dropboxcx-freeze

cx_freeze a python script including dropbox


I want to freeze a python script which is using dropbox to upload a file. I am using python 2.7 and windows 7. If I try just this example code:

import dropbox
client = dropbox.client.DropboxClient(<authtoken>)
client.put_file('FileName',"", overwrite=True)

I created an app with dropbox and generated a token which is explained on the dropbox homepage. The example works if I just use the python script. For future applications I want to freeze the script to use it on computers without python. If I execute the .exe file I get the error message below, I named the python script dropbox.py:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "dropbox.py", line 2, in <module>
File "H:\dropbox.py", line 3, in <module>
client = dropbox.client.DropboxClient("authToken")
AttributeError: 'module' object has no attribute 'client'

Solved: Do not use dropbox.py for your example scripts The error states that it can't find the client module, but how can fix this error? My setup.py file is:

import cx_Freeze
import sys
base = None

executables = [
    cx_Freeze.Executable("dropbox.py", base = base),
    ]

build_exe_options = {"includes":[],
                     "include_files":[],
                     "excludes":[],
                     "packages":[]
                     }
cx_Freeze.setup(
    name = "script",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "A basic example",
    executables = executables)

I also tried to add in packages "dropbox" but it doesn't work.

I hope someone can help me. Maybe there is another way to upload a file to dropbox?

Cheers Max

Edit1: Indeed it was a problem with the name of my example script. Though it still doesn't work. The new error is:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "mydropbox.py", line 2, in <module>
  File "C:\Python27\lib\site-packages\dropbox\__init__.py", line 3, in <module>
    from . import client, rest, session
  File "C:\Python27\lib\site-packages\dropbox\client.py", line 22, in <module>
    from .rest import ErrorResponse, RESTClient, params_to_urlencoded
  File "C:\Python27\lib\site-packages\dropbox\rest.py", line 26, in <module>
    TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
  File "build\bdist.win32\egg\pkg_resources.py", line 950, in resource_filename
  File "build\bdist.win32\egg\pkg_resources.py", line 1638, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip

I tried to solve the error with this site:

enter link description here

but doesn't work. Does anybody has a solution regarding my new error?


Solution

  • I've solved the problem this way:

    1) In your dropbox module location find the file "rest.py" (C:\Python27\Lib\site-packages\dropbox-2.2.0-py2.7.egg\dropbox - in my case)

    2) Comment the string TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')

    3) You can either write

    TRUSTED_CERT_FILE = 'trusted-certs.crt' (I've used this way) or

    if hasattr(sys, 'frozen'):

    TRUSTED_CERT_FILE = 'trusted-certs.crt'

    else:

    TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')

    4) Remove all pyc files from dropbox directory and run the py file with your program

    5) Rebuild your executable with cx_freeze

    6) Copy trusted-certs.crt file from dropbox directory into library.zip/dropbox

    7) Enjoy!