项目下创建一个文件,autoinstall.py ,复制下面的代码,在项目最开始加入import autoinstall,直接运行项目即可。
import sys
import os
from importlib import import_module
class AutoInstall():
_loaded = set()
@classmethod
def find_spec(cls, name, path, target=None):
if path is None and name not in cls._loaded:
cls._loaded.add(name)
print("Installing", name)
try:
result = os.system('pip install {}'.format(name))
if result == 0:
return import_module(name)
except Exception as e:
print("Failed", e)
return None
sys.meta_path.append(AutoInstall)