Search code examples
pythonpy2exegspreadoauth2client

py2exe missing modules: oauth2client.client and gspread modules


I have created the following Python script using the gspread and oauth2 modules

import gspread
from oauth2client.client import SignedJwtAssertionCredentials

credentials = SignedJwtAssertionCredentials("client_email","private_key", "https://spreadsheets.google.com/feeds")
gc = gspread.authorize(credentials)

spreadsheet = gc.open_by_key("spreadsheet_id")
worksheet = spreadsheet.worksheet("sheet_name")
lstoforders = worksheet.get_all_values()

...some extra code...

When I run this code as a .py file everything works smoothly. However, when I try to package it into an executable Windows program using py2exe, I get the following output

The following modules appear to be missing
['ElementC14N', 'IronPythonConsole', 'System', 'System.Windows.Forms.Clipboard', '_scproxy', 'ca_certs_locater', 'clr', 'console', 'email.FeedParser', 'email.Message', 'email.Utils', 'google.appengine.api', 'google.appengine.api.urlfetch','google3.apphosting.api', 'google3.apphosting.api.urlfetch', 'http', 'modes.editingmodes', 'oauth2client.client' 'pyreadline.keysyms.make_KeyPress', 'pyreadline.keysyms.make_KeyPress_from_keydescr', 'pyreadline.keysyms.make_keyinfo', 'pyreadline.keysyms.make_keysym', 'startup', 'urllib.parse']

Accordingly, when I try to run the resulting exe file, I get the following error

Traceback (most recent call last):
File "gspread_to_autocomplete_json.py", line 2, in <module> ImportError: No module named oauth2client.client

It appears as if py2exe cannot find the gspread and oauth2client.client modules. These modules are installed on my machine.

Does anybody have a clue why this is happening?

Thanks.

Nicola


Solution

  • You can choose in your setup.py with packages and modules you want to include.It might be that your setup script is not finding all the dependencies automatically (actually it is pretty common).Try to have a look at the answer I gave to this question.

    Add options to your setup.py

    You can also use more py2exe options in order that you are importing all the modules and the packages required by your project. E.g.

    # setup.py
    from distutils.core import setup
    import py2exe
    setup(console=["script.py"],
          options={
                  "py2exe":{
                        "optimize": 2,
                        "includes": ["mf1.py", "mf2.py", "mf3.py"], # List of all the modules you want to import
                        "packages": ["package1"] # List of the package you want to make sure that will be imported
                   }
           }
        )
    

    In this way you can force the import of the missing script of your project