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 mentioned in the file are present. If some packages are missing, how to find out which are the missing packages?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can create a virtualenv with access to the system site packages and test check whether the package (or another dependencies) are installed or not. This way the packages are not really installed (if you just want to check). An example using virtualenv wrapper would be:
The pythonic way of doing it is via the
pkg_resources
API. The requirements are written in a format understood by setuptools. E.g:The example code:
You can run
pip freeze
to see what you have installed and compare it to yourrequirements.txt
file.If you want to install missing modules you can run
pip install -r requirements.txt
and that will install any missing modules and tell you at the end which ones were missing and installed.If requirements.txt is like :
Then the following script will tell you which modules are missing :
This would print :
Based on the answer by Zaur, assuming you indeed use a
requirements.txt
file, you may want a unit test, perhaps intests/test_requirements.py
, that confirms the availability of packages.This approach uses a subtest to independently confirm each requirement. This is useful so that all failures are documented.