Check if my Python has all required packages

后端 未结 7 1878
悲&欢浪女
悲&欢浪女 2020-12-23 09:53

I have a requirements.txt file with a list of packages that are required for my virtual environment. Is it possible to find out whether all the packages mention

7条回答
  •  星月不相逢
    2020-12-23 10:39

    If requirements.txt is like :

    django
    oursql
    sys
    notexistingmodule
    

    Then the following script will tell you which modules are missing :

    #!/usr/bin/python3
    fname = 'requirements.txt'
    with open(fname, 'r', encoding='utf-8') as fhd:
        for line in fhd:
            try:
                exec("import " + line)
            except:
                print("[ERROR] Missing module:", line)
    

    This would print :

    [ERROR] Missing module: notexistingmodule
    

提交回复
热议问题