Python 2에서 가져올 수 있는지 확인하려면 try...except와 함께 imp 모듈을 사용할 수 있습니다. 예를 들어,
import imp
try:
imp.find_module('eggs')
found = True
except ImportError:
found = False
print found 다음과 같은 결과가 표시됩니다.
False
또한 pkgutil 모듈의 iter_modules를 사용하여 모든 모듈을 반복하여 지정된 모듈이 있는지 확인할 수 있습니다. 예를 들어,
from pkgutil import iter_modules
def module_exists(module_name):
return module_name in (name for loader, name, ispkg in iter_modules())
print module_exists('scrapy') 이것은 출력을 줄 것입니다:
True
내 PC에 이 모듈이 설치되어 있기 때문입니다.
또는 쉘에서 확인하려는 경우 다음을 사용할 수 있습니다.
python -c "help('modules');" | grep yourmodule