Last active
March 24, 2024 06:07
-
-
Save 44hero/c7ac2702d003e0a4789c8ba31cd9dc54 to your computer and use it in GitHub Desktop.
from .. import YO_jointDrawStyle_change
from .. import YO_jointRadiusSlider
としています。
現状は、存在しなければ、エラーでプログラムが止まってしまいます。
存在しないくても、エラーでプログラムが止まらずに、そのまま進んでもらうためには、
どういった記述を追記する必要がありますか?
ChatGPT
Pythonでは、モジュールやパッケージが存在しない場合でもエラーが発生しないようにするために、try-exc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .. import YO_jointDrawStyle_change | |
from .. import YO_jointRadiusSlider | |
としています。 | |
現状は、存在しなければ、エラーでプログラムが止まってしまいます。 | |
存在しないくても、エラーでプログラムが止まらずに、そのまま進んでもらうためには、 | |
どういった記述を追記する必要がありますか? | |
ChatGPT | |
Pythonでは、モジュールやパッケージが存在しない場合でもエラーが発生しないようにするために、try-except文を使用することができます。具体的には、tryブロック内でインポート文を実行し、ModuleNotFoundErrorなどの例外が発生した場合にexceptブロックで処理を行います。 | |
以下は、その方法を示したコード例です: | |
python | |
Copy code | |
try: | |
from .. import YO_jointDrawStyle_change | |
from .. import YO_jointRadiusSlider | |
except ModuleNotFoundError: | |
# エラーが発生した場合の処理(何もしない場合もあります) | |
pass | |
このようにすることで、Y0_jointDrawStyle_changeとYO_jointRadiusSliderが存在しない場合でも、プログラムが停止することなくそのまま進むことができます。 | |
また、 | |
Pythonの例外処理では、except節が複数ある場合、順番は重要です。 | |
例外は上から下に順番に評価され、最初にマッチしたものが実行されます。 | |
そのため、より具体的な例外を先に書くべきです。 | |
ModuleNotFoundError は ImportError のサブクラスなので、 | |
except ModuleNotFoundError | |
を先に書くことが一般的です。 | |
しかし、Python 3.6以降のバージョンでは | |
、ModuleNotFoundError が ImportError のサブクラスであるため、 | |
両方の例外を同じように扱うことができます。 | |
try: | |
# インポート文 | |
except ModuleNotFoundError: | |
# モジュールが見つからない場合の処理 | |
except ImportError: | |
# その他のインポートエラーの処理 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```eachTool_config.py | |
# -*- coding: utf-8 -*- | |
parentDir = 'YO_utilityTools' | |
# 各ツール ################################################################ start | |
# モジュール名の辞書 | |
YO_renameTool5_main = 'renameTool.YO_renameTool5_main' | |
createSpaceNode = 'createSpaceNode.YO_createSpaceNode3_main' | |
YO_nodeCreateToWorldSpace = 'YO_nodeCreateToWorldSpace' | |
createClusterAndRenameTool = 'createClusterAndRenameTool.YO_createClusterAndRename6_main' | |
createSpIkAndRenameTool = 'createSpIkAndRenameTool.YO_createSpIkAndRename3_main' | |
pointConstraintByMatrix = "pointConstraintByMatrix.YO_pointConstraintByMatrix1_main" | |
orientConstraintByMatrix = "orientConstraintByMatrix.YO_orientConstraintByMatrix1_main" | |
scaleConstraintByMatrix = "scaleConstraintByMatrix.YO_scaleConstraintByMatrix1_main" | |
shearConstraintByMatrix = "shearConstraintByMatrix.YO_shearConstraintByMatrix1_main" | |
YO_parentConstraintByMatrix5 = "YO_parentConstraintByMatrix5" | |
YO_parentConstraintByMatrix62 = "YO_parentConstraintByMatrix62" | |
YO_jointDrawStyle_change = 'YO_jointDrawStyle_change' | |
YO_jointRadiusSlider = 'YO_jointRadiusSlider' | |
tool_dict = { | |
YO_renameTool5_main: None, | |
createSpaceNode: None, | |
YO_nodeCreateToWorldSpace: None, | |
createClusterAndRenameTool: None, | |
createSpIkAndRenameTool: None, | |
pointConstraintByMatrix: None, | |
orientConstraintByMatrix: None, | |
scaleConstraintByMatrix: None, | |
shearConstraintByMatrix: None, | |
YO_parentConstraintByMatrix5: None, | |
YO_parentConstraintByMatrix62: None, | |
YO_jointDrawStyle_change: None, | |
YO_jointRadiusSlider: None | |
} | |
# 各ツール ################################################################ end | |
``` | |
読み込み先では、 | |
```yoRigGenericToolGroup_Modl.py | |
# 各ツールの読み込み | |
# 同階層にある、データベース用モジュール eachTool_config.py が存在するディレクトリのパスの設定 | |
current_file_path = os.path.abspath(inspect.getfile(inspect.currentframe())) | |
# print(f"Current file path: \n\t{current_file_path}") | |
directoryFullPath = os.path.dirname(current_file_path) | |
parentDirFullPath = os.path.abspath(os.path.join(directoryFullPath, os.pardir)) | |
fileName = os.path.basename(current_file_path) # 実行中のファイル名の取得 | |
directory = directoryFullPath.split('\\', )[-1] # \ で分割したリストの、最後のみ取得 | |
# eachTool_config.pyが存在するディレクトリのパスの取得 | |
# print(directoryFullPath) | |
# ディレクトリをインポートパスに追加 | |
if directoryFullPath not in sys.path: | |
sys.path.insert(0, directoryFullPath) | |
from eachTool_config import parentDir, tool_dict | |
# 各ツールモジュール のインポート | |
for module_path, func_name in tool_dict.items(): | |
try: | |
module = __import__(f'{parentDir}.{module_path}', fromlist = [func_name]) | |
globals()[module_path.split('.')[-1]] = module | |
# om.MGlobal.displayInfo(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<OK> result: exist, and loaded.\n' | |
# ) | |
except (ModuleNotFoundError, ImportError): | |
globals()[module_path.split('.')[-1]] = None | |
# om.MGlobal.displayWarning(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<NG> result: not exist, and loading not possible.\n' | |
# ) | |
``` | |
で、上手くいっていますが、 | |
IDEが、 | |
参照 'YO_createSpaceNode3_main' は未解決です | |
参照 'YO_nodeCreateToWorldSpace' は未解決です | |
参照 'YO_createClusterAndRename6_main' は未解決です | |
参照 'YO_createSpIkAndRename3_main' は未解決です | |
参照 'YO_pointConstraintByMatrix1_main' は未解決です | |
参照 'YO_orientConstraintByMatrix1_main' は未解決です | |
参照 'YO_scaleConstraintByMatrix1_main' は未解決です | |
参照 'YO_shearConstraintByMatrix1_main' は未解決です | |
参照 'YO_parentConstraintByMatrix5' は未解決です | |
参照 'YO_parentConstraintByMatrix62' は未解決です | |
参照 'YO_jointDrawStyle_change' は未解決です | |
参照 'YO_jointRadiusSlider' は未解決です | |
と出します。 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parentDir = 'YO_utilityTools' | |
def sendWarningInformation(moduleNameByString: str): | |
om.MGlobal.displayWarning(f'{parentDir}.{moduleNameByString}' | |
u'\n\t' + u':<NG> result: 上記の 所定の箇所 に、\n\t\t' | |
f'{moduleNameByString}' | |
u'\n\t\t\tが存在しません。' | |
u'よって、ロードしませんでした。\n' | |
) | |
# 各ツールの読み込み ############################################################## start | |
# 以下、若干冗長な記述ですが、現状では、一番保守しやすい記述です | |
try: | |
from ..renameTool import YO_renameTool5_main | |
except (ModuleNotFoundError, ImportError): | |
YO_renameTool5_main = None | |
sendWarningInformation('YO_renameTool5_main') | |
try: | |
from ..createSpaceNode import YO_createSpaceNode3_main | |
except (ModuleNotFoundError, ImportError): | |
YO_createSpaceNode3_main = None | |
sendWarningInformation('YO_createSpaceNode3_main') | |
try: | |
from .. import YO_nodeCreateToWorldSpace | |
except (ModuleNotFoundError, ImportError): | |
YO_nodeCreateToWorldSpace = None | |
sendWarningInformation('YO_nodeCreateToWorldSpace') | |
try: | |
from ..createClusterAndRenameTool import YO_createClusterAndRename6_main | |
except (ModuleNotFoundError, ImportError): | |
YO_createClusterAndRename6_main = None | |
sendWarningInformation('YO_createClusterAndRename6_main') | |
try: | |
from ..createSpIkAndRenameTool import YO_createSpIkAndRename3_main | |
except (ModuleNotFoundError, ImportError): | |
YO_createSpIkAndRename3_main = None | |
sendWarningInformation('YO_createSpIkAndRename3_main') | |
try: | |
from ..pointConstraintByMatrix import YO_pointConstraintByMatrix1_main | |
except (ModuleNotFoundError, ImportError): | |
YO_pointConstraintByMatrix1_main = None | |
sendWarningInformation('YO_pointConstraintByMatrix1_main') | |
try: | |
from ..orientConstraintByMatrix import YO_orientConstraintByMatrix1_main | |
except (ModuleNotFoundError, ImportError): | |
YO_orientConstraintByMatrix1_main = None | |
sendWarningInformation('YO_orientConstraintByMatrix1_main') | |
try: | |
from ..scaleConstraintByMatrix import YO_scaleConstraintByMatrix1_main | |
except (ModuleNotFoundError, ImportError): | |
YO_scaleConstraintByMatrix1_main = None | |
sendWarningInformation('YO_scaleConstraintByMatrix1_main') | |
try: | |
from ..shearConstraintByMatrix import YO_shearConstraintByMatrix1_main | |
except (ModuleNotFoundError, ImportError): | |
YO_shearConstraintByMatrix1_main = None | |
sendWarningInformation('YO_shearConstraintByMatrix1_main') | |
try: | |
from .. import YO_parentConstraintByMatrix5 | |
except (ModuleNotFoundError, ImportError): | |
YO_parentConstraintByMatrix5 = None | |
sendWarningInformation('YO_parentConstraintByMatrix5') | |
try: | |
from .. import YO_parentConstraintByMatrix62 | |
except (ModuleNotFoundError, ImportError): | |
YO_parentConstraintByMatrix62 = None | |
sendWarningInformation('YO_parentConstraintByMatrix62') | |
try: | |
from .. import YO_jointDrawStyle_change | |
except (ModuleNotFoundError, ImportError): | |
YO_jointDrawStyle_change = None | |
sendWarningInformation('YO_jointDrawStyle_change') | |
try: | |
from .. import YO_jointRadiusSlider | |
except (ModuleNotFoundError, ImportError): | |
YO_jointRadiusSlider = None | |
sendWarningInformation('YO_jointRadiusSlider') | |
# 各ツールの読み込み ############################################################## end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```eachTool_config.py | |
# -*- coding: utf-8 -*- | |
parentDir = 'YO_utilityTools' | |
# 各ツール ################################################################ start | |
# モジュール名の辞書 | |
YO_renameTool5_main = 'renameTool.YO_renameTool5_main' | |
createSpaceNode = 'createSpaceNode.YO_createSpaceNode3_main' | |
YO_nodeCreateToWorldSpace = 'YO_nodeCreateToWorldSpace' | |
createClusterAndRenameTool = 'createClusterAndRenameTool.YO_createClusterAndRename6_main' | |
createSpIkAndRenameTool = 'createSpIkAndRenameTool.YO_createSpIkAndRename3_main' | |
pointConstraintByMatrix = "pointConstraintByMatrix.YO_pointConstraintByMatrix1_main" | |
orientConstraintByMatrix = "orientConstraintByMatrix.YO_orientConstraintByMatrix1_main" | |
scaleConstraintByMatrix = "scaleConstraintByMatrix.YO_scaleConstraintByMatrix1_main" | |
shearConstraintByMatrix = "shearConstraintByMatrix.YO_shearConstraintByMatrix1_main" | |
YO_parentConstraintByMatrix5 = "YO_parentConstraintByMatrix5" | |
YO_parentConstraintByMatrix62 = "YO_parentConstraintByMatrix62" | |
YO_jointDrawStyle_change = 'YO_jointDrawStyle_change' | |
YO_jointRadiusSlider = 'YO_jointRadiusSlider' | |
tool_dict = { | |
YO_renameTool5_main: None, | |
createSpaceNode: None, | |
YO_nodeCreateToWorldSpace: None, | |
createClusterAndRenameTool: None, | |
createSpIkAndRenameTool: None, | |
pointConstraintByMatrix: None, | |
orientConstraintByMatrix: None, | |
scaleConstraintByMatrix: None, | |
shearConstraintByMatrix: None, | |
YO_parentConstraintByMatrix5: None, | |
YO_parentConstraintByMatrix62: None, | |
YO_jointDrawStyle_change: None, | |
YO_jointRadiusSlider: None | |
} | |
# 各ツール ################################################################ end | |
``` | |
読み込み先では、 | |
```yoRigGenericToolGroup_Modl.py | |
# 各ツールの読み込み | |
# 同階層にある、データベース用モジュール eachTool_config.py が存在するディレクトリのパスの設定 | |
current_file_path = os.path.abspath(inspect.getfile(inspect.currentframe())) | |
# print(f"Current file path: \n\t{current_file_path}") | |
directoryFullPath = os.path.dirname(current_file_path) | |
parentDirFullPath = os.path.abspath(os.path.join(directoryFullPath, os.pardir)) | |
fileName = os.path.basename(current_file_path) # 実行中のファイル名の取得 | |
directory = directoryFullPath.split('\\', )[-1] # \ で分割したリストの、最後のみ取得 | |
# eachTool_config.pyが存在するディレクトリのパスの取得 | |
# print(directoryFullPath) | |
# ディレクトリをインポートパスに追加 | |
if directoryFullPath not in sys.path: | |
sys.path.insert(0, directoryFullPath) | |
from eachTool_config import parentDir, tool_dict | |
# 各ツールモジュール のインポート | |
for module_path, func_name in tool_dict.items(): | |
try: | |
module = __import__(f'{parentDir}.{module_path}', fromlist = [func_name]) | |
globals()[module_path.split('.')[-1]] = module | |
# om.MGlobal.displayInfo(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<OK> result: exist, and loaded.\n' | |
# ) | |
except (ModuleNotFoundError, ImportError): | |
globals()[module_path.split('.')[-1]] = None | |
# om.MGlobal.displayWarning(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<NG> result: not exist, and loading not possible.\n' | |
# ) | |
``` | |
で、上手くいっていますが、 | |
IDEが、 | |
参照 'YO_createSpaceNode3_main' は未解決です | |
参照 'YO_nodeCreateToWorldSpace' は未解決です | |
参照 'YO_createClusterAndRename6_main' は未解決です | |
参照 'YO_createSpIkAndRename3_main' は未解決です | |
参照 'YO_pointConstraintByMatrix1_main' は未解決です | |
参照 'YO_orientConstraintByMatrix1_main' は未解決です | |
参照 'YO_scaleConstraintByMatrix1_main' は未解決です | |
参照 'YO_shearConstraintByMatrix1_main' は未解決です | |
参照 'YO_parentConstraintByMatrix5' は未解決です | |
参照 'YO_parentConstraintByMatrix62' は未解決です | |
参照 'YO_jointDrawStyle_change' は未解決です | |
参照 'YO_jointRadiusSlider' は未解決です | |
と出します。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```eachTool_config.py | |
# -*- coding: utf-8 -*- | |
parentDir = 'YO_utilityTools' | |
# 各ツール ################################################################ start | |
# モジュール名の辞書 | |
YO_renameTool5_main = 'renameTool.YO_renameTool5_main' | |
createSpaceNode = 'createSpaceNode.YO_createSpaceNode3_main' | |
YO_nodeCreateToWorldSpace = 'YO_nodeCreateToWorldSpace' | |
createClusterAndRenameTool = 'createClusterAndRenameTool.YO_createClusterAndRename6_main' | |
createSpIkAndRenameTool = 'createSpIkAndRenameTool.YO_createSpIkAndRename3_main' | |
pointConstraintByMatrix = "pointConstraintByMatrix.YO_pointConstraintByMatrix1_main" | |
orientConstraintByMatrix = "orientConstraintByMatrix.YO_orientConstraintByMatrix1_main" | |
scaleConstraintByMatrix = "scaleConstraintByMatrix.YO_scaleConstraintByMatrix1_main" | |
shearConstraintByMatrix = "shearConstraintByMatrix.YO_shearConstraintByMatrix1_main" | |
YO_parentConstraintByMatrix5 = "YO_parentConstraintByMatrix5" | |
YO_parentConstraintByMatrix62 = "YO_parentConstraintByMatrix62" | |
YO_jointDrawStyle_change = 'YO_jointDrawStyle_change' | |
YO_jointRadiusSlider = 'YO_jointRadiusSlider' | |
tool_dict = { | |
YO_renameTool5_main: None, | |
createSpaceNode: None, | |
YO_nodeCreateToWorldSpace: None, | |
createClusterAndRenameTool: None, | |
createSpIkAndRenameTool: None, | |
pointConstraintByMatrix: None, | |
orientConstraintByMatrix: None, | |
scaleConstraintByMatrix: None, | |
shearConstraintByMatrix: None, | |
YO_parentConstraintByMatrix5: None, | |
YO_parentConstraintByMatrix62: None, | |
YO_jointDrawStyle_change: None, | |
YO_jointRadiusSlider: None | |
} | |
# 各ツール ################################################################ end | |
``` | |
読み込み先では、 | |
```yoRigGenericToolGroup_Modl.py | |
# 各ツールの読み込み | |
# 同階層にある、データベース用モジュール eachTool_config.py が存在するディレクトリのパスの設定 | |
current_file_path = os.path.abspath(inspect.getfile(inspect.currentframe())) | |
# print(f"Current file path: \n\t{current_file_path}") | |
directoryFullPath = os.path.dirname(current_file_path) | |
parentDirFullPath = os.path.abspath(os.path.join(directoryFullPath, os.pardir)) | |
fileName = os.path.basename(current_file_path) # 実行中のファイル名の取得 | |
directory = directoryFullPath.split('\\', )[-1] # \ で分割したリストの、最後のみ取得 | |
# eachTool_config.pyが存在するディレクトリのパスの取得 | |
# print(directoryFullPath) | |
# ディレクトリをインポートパスに追加 | |
if directoryFullPath not in sys.path: | |
sys.path.insert(0, directoryFullPath) | |
from eachTool_config import parentDir, tool_dict | |
# eachTool_config.py の存在の有無も考慮して。。。 | |
try: | |
from eachTool_config import parentDir, tool_dict | |
# 各ツールモジュール のインポート | |
for module_path, func_name in tool_dict.items(): | |
try: | |
module = __import__(f'{parentDir}.{module_path}', fromlist = [func_name]) | |
globals()[module_path.split('.')[-1]] = module | |
# om.MGlobal.displayInfo(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<OK> result: exist, and loaded.\n' | |
# ) | |
except (ModuleNotFoundError, ImportError): | |
globals()[module_path.split('.')[-1]] = None | |
# om.MGlobal.displayWarning(f'{parentDir}.{module_path}' | |
# u'\n\t' + u':<NG> result: not exist, and loading not possible.\n' | |
# ) | |
except (ModuleNotFoundError, ImportError): | |
om.MGlobal.displayWarning('各ツールの読み込み 専用の、' | |
'データベース用モジュール eachTool_config.py' | |
'が見つかりません。') | |
``` | |
で、上手くいっていますが、 | |
IDEが、 | |
参照 'YO_createSpaceNode3_main' は未解決です | |
参照 'YO_nodeCreateToWorldSpace' は未解決です | |
参照 'YO_createClusterAndRename6_main' は未解決です | |
参照 'YO_createSpIkAndRename3_main' は未解決です | |
参照 'YO_pointConstraintByMatrix1_main' は未解決です | |
参照 'YO_orientConstraintByMatrix1_main' は未解決です | |
参照 'YO_scaleConstraintByMatrix1_main' は未解決です | |
参照 'YO_shearConstraintByMatrix1_main' は未解決です | |
参照 'YO_parentConstraintByMatrix5' は未解決です | |
参照 'YO_parentConstraintByMatrix62' は未解決です | |
参照 'YO_jointDrawStyle_change' は未解決です | |
参照 'YO_jointRadiusSlider' は未解決です | |
と出します。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment