py2exe缺少模块:oauth2client.client和gspread模块

时间:2016-01-13 10:51:09

标签: python py2exe gspread oauth2client

我使用gspread和oauth2模块创建了以下Python脚本

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...

当我将此代码作为.py文件运行时,一切运行顺利。但是,当我尝试使用py2exe将其打包成可执行的Windows程序时,我得到以下输出

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

因此,当我尝试运行生成的exe文件时,出现以下错误

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

好像py2exe似乎找不到gspread和oauth2client.client模块。这些模块安装在我的机器上。

有人知道为什么会这样吗?

感谢。

尼古拉

1 个答案:

答案 0 :(得分:0)

您可以在setup.py中选择要包含的软件包和模块。可能是您的安装脚本没有自动查找所有依赖项(实际上它很常见)。试着看看{{ 3}}

  

向setup.py添加选项

您还可以使用更多the answer I gave to this question,以便导入项目所需的所有模块和包。 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
               }
       }
    )

通过这种方式,您可以强制导入项目缺失的脚本