質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
xlwings

xlwingsは、ExcelをPythonで操作するためのオープンソースライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

1199閲覧

AttributeError: 'NoneType' object has no attribute 'apps'で処理が止まる。

Y.K

総合スコア2

xlwings

xlwingsは、ExcelをPythonで操作するためのオープンソースライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/11/23 06:25

編集2022/11/23 06:30

前提

AttributeError: 'NoneType' object has no attribute 'apps'で処理が止まる。

実現したいこと

Windows 11 & Python 3.10.8の環境で最新 xlwings ライブラリを使っています。
ソース.pyの実体からや、cx_Freezeを使用したexeファイルを作成して、目的の動作が行われている事を確認しています。
比較のためpy2exeを使って同様にexeファイルを作成したところ、distフォルダの中には実行ファイル群が作成されていたのですが、作成したソース.exeを実行すると、Excel処理の箇所でエラーログが吐き出されて動作が止まってしまいます。

発生している問題・エラーメッセージ

Traceback (most recent call last):
File "hoge.py", line 666, in <module>
File "hoge.py", line 404, in hogeExcel
File "xlwings\main.pyc", line 294, in init
AttributeError: 'NoneType' object has no attribute 'apps'

hoge.pyソース内の 666行目 & 404行目の内容は
rf = hogeExcel(True) → 666行目(モジュール呼び出し)

def hogeExcel(bool):
app = xw.App(visible = False, add_book = False)  → 404行目

xlwingsのライブラリ処理の中でエラーが吐き出されているようですが、py2exeってxlwingsとの相性が悪いのでしょうか?
それとも私のソース内での書き方が悪いのか、ビルドするスクリプトの記述に問題があるのか、よく分からない部分が多いので質問させて頂きました。

ビルドするpy2exeのスクリプト内容です

from distutils.core import setup
from setuptools import find_packages
import py2exe

option = {
'compressed': 1,
'optimize': 2,
'bundle_files': 1,
}

setup(
options = {
'py2exe': option,
},
windows = [
{'script': 'hoge.py',
'icon_resources': [(1, 'hoge.ico')]
},
],
py_modules = [],
zipfile = 'lib\libs.zip',
)

ビルド処理しているリストでは、以下の通りxlwingsでのメッセージが多いようですが、これで正常なのでしょうか?
※環境を変えて 3.11 でビルドしましたが、3.10.8でも同じ結果です。
先にも書きましたが、実行ファイル群は作成されています。

PS C:\Users\ore> python setup.py py2exe

setup(

44 missing Modules
? Foundation imported from send2trash.plat_osx_pyobjc
? PIL imported from PySimpleGUI.PySimpleGUI, xlwings._xlmac, xlwings._xlwindows, xlwings.main, xlwings.pro.reports.main
? PIL.Image imported from xlwings.pro.reports.main
? main imported from bdb, pdb
? _frozen_importlib imported from importlib, importlib.abc, zipimport
? _frozen_importlib_external imported from importlib, importlib._bootstrap, importlib.abc, zipimport
? _posixshmem imported from multiprocessing.resource_tracker, multiprocessing.shared_memory
? _winreg imported from platform
? aem imported from xlwings._xlmac
? appscript imported from xlwings._xlmac
? appscript.reference imported from xlwings._xlmac
? asyncio.DefaultEventLoopPolicy imported from -
? asyncio.get_event_loop imported from xlwings.udfs
? asyncio.get_running_loop imported from xlwings.udfs
? conversion.Converter imported from xlwings.pro.reports.markdown
? cryptography.fernet imported from xlwings.pro.utils
? dummy.Process imported from multiprocessing.pool
? gi.repository imported from send2trash.plat_gio
? java.lang imported from platform
? jinja2 imported from xlwings.pro.reports.main
? matplotlib imported from xlwings.main, xlwings.utils
? matplotlib.backends.backend_agg imported from xlwings.main
? matplotlib.figure imported from xlwings.pro.reports.main, xlwings.utils
? matplotlib.pyplot imported from xlwings.utils
? numpy imported from xlwings._xlmac, xlwings._xlwindows, xlwings.conversion, xlwings.conversion.numpy_conv, xlwings.conversion.standard, xlwings.pro._xlcalamine, xlwings.pro._xlremote, xlwings.pro.reports.filters, xlwings.pro.reports.main, xlwings.utils
? org.python.core imported from copy, pickle
? os.path imported from ctypes._aix, distutils.file_util, os, pkgutil, py_compile, send2trash.plat_other, send2trash.plat_win_legacy, send2trash.plat_win_modern, sysconfig, tracemalloc, unittest, unittest.util, xlwings.udfs
? osax imported from xlwings._xlmac
? pandas imported from xlwings._xlmac, xlwings._xlwindows, xlwings.conversion, xlwings.conversion.numpy_conv, xlwings.conversion.pandas_conv, xlwings.main, xlwings.pro._xlcalamine, xlwings.pro._xlremote, xlwings.pro.reports.main
? pdfrw imported from xlwings.pro.reports.pdf
? plotly imported from xlwings.pro.reports.main
? plotly.graph_objects imported from xlwings.utils
? plugins.PLUGINS imported from xlwings.mistune
? pro.Markdown imported from xlwings.conversion.standard
? pro.reports.render_template imported from xlwings.main
? pro.verify_execute_permission imported from xlwings.udfs
? psutil imported from xlwings._xlmac
? readline imported from cmd, code, pdb
? reports.Markdown imported from xlwings.pro
? reports.MarkdownStyle imported from xlwings.pro
? requests imported from xlwings.pro.module_permissions
? resource imported from test.support
? urllib.quote imported from send2trash.plat_other, xlwings.mistune.util
? win32com.gen_py imported from win32com
Building 'dist\hoge.exe'.
Building shared code archive 'dist\lib\libs.zip'.
Copy DLL C:\Users\ore\AppData\Local\Programs\Python\Python311\DLLs\tcl86t.dll to dist
Copy DLL C:\Users\ore\AppData\Local\Programs\Python\Python311\DLLs\libcrypto-1_1.dll to dist
Copy DLL C:\Users\ore\AppData\Local\Programs\Python\Python311\DLLs\libffi-8.dll to dist
Copy DLL C:\Users\ore\AppData\Local\Programs\Python\Python311\DLLs\libssl-1_1.dll to dist
Copy DLL C:\Users\ore\AppData\Local\Programs\Python\Python311\DLLs\tk86t.dll to dist
PS C:\Users\ore>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Y.K

2022/11/24 23:36

自己解決しました。 ビルドするpy2exeのスクリプト内容の'bundle_files': 1 → "3" に変更したところ正常動作したようです。 bundle_files bundle dlls in the zipfile or the exe. Valid values for bundle_files are: 3 = don't bundle (default) 2 = bundle everything but the Python interpreter 1 = bundle everything, including the Python interpreter デフォルトの設定で良かったようです。 この設定で動きが変わるとは・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問