代码如下,Windows环境测试通过
import os import platform import re import sys from pathlib import Path def is_venv() -> bool: """判断是否处于虚拟环境(也适用于poetry的)""" if hasattr(sys, "real_prefix"): return True return hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix def run_and_echo(cmd) -> int: print("-->\n", cmd, flush=True) return os.system(cmd) def install_and_rerun(*packages): py = Path(sys.executable) if not is_venv(): if not (p := Path("venv")).exists(): if run_and_echo(f"{py} -m venv venv"): return 1 if platform.platform().lower().startswith("win"): py = p / "Scripts" / "python.exe" else: py = p / "bin/python" if run_and_echo(f"{py} -m pip install {' '.join(packages)}"): return 2 cmd = f"{py} {sys.argv[0]} --no-install {' '.join(sys.argv[1:])}" return run_and_echo(cmd) try: import kivy except ImportError as e: if "--no-install" in sys.argv: raise e modules = re.findall(r"'([a-zA-Z_-]+)'", str(e)) sys.exit(install_and_rerun(*modules)) def main(): pass if __name__ == '__main__': main()
运行时,如果import失败,会判断是否处于虚拟环境,是的话,直接pip install报错的缺失包,然后自动重新执行脚本;
否则,判断当前路径是否有venv文件,有的话使用venv/*/python,否则使用python -m venv venv创建它
标签:venv,return,依赖,python,sys,虚拟环境,install,import From: https://www.cnblogs.com/waketzheng/p/17486202.html