Python VSCode ModuleNotFoundErrorが起きた場合

Python Select Interpreter ModuleNotFoundError

VSCodeを使ってPythonのスクリプト便利ですよね。昔ながらのF5デバッグ実行もできるし。

そんなVSCodeで、ある日突然ModuleNotFoundErrorが出て、Pythonモジュールが読込できなくなりました。エラーは下記です。

例外が発生しました: ModuleNotFoundError
No module named 'openpyxl'

エディターを見るとimportの行が波線マーク

importが波線

調べてみるとInterpreter設定のパスが、何かの影響で変わってしまったようでした

そのInterpreter設定を変更する方法は、VSCode上で[F1]を押し「Python: Slect Interpreter」を選択します。

Python: Select Interpreter

パスが選択できるので私の場合「/usr/local/bin/python3」を選択

/usr/local/bin/python3を選択

上記の方法でVSCodeでのModuleNotFoundErrorは回避できました

AppleScript argv 引数

osascript シェル実行

AppleScript でスクリプトで引数を受け取りたい場合のコード例です

on run argv
    argv as text
end

上記コードをファイルに保存し(ここではindex.scptとしました) 「/usr/bin/osascript」から実行すると結果「ab」が返ります

/usr/bin/osascript index.scpt a b
/usr/bin/osascript index.scpt a b

引数をカンマ区切りで返す場合下記のように記述できます

on run argv
	set _d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	try
		set _a to argv as text
	on error
		set _a to "error"
	end try
	set AppleScript's text item delimiters to _d
	_a
end run

AppleScriptをシェル実行したい場合

osascriptから実行しなくてもAppleScriptはシェルに入れることができます。その場合先頭に「#!/usr/bin/env osascript」が必要です。

#!/usr/bin/env osascript
on run argv
    argv as text 
end

上記のコードをファイル保存し、chmodで実行権限を与えると実行可能です。

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED