I'm trying to fix up one of my virtualenv (virtual environment) - I'd like to reset all of the installed libraries back to the ones that match production.
Is there a quick and easy way to do this with pip?
Use the below command based on your OS:
For Linux Machine:
pip freeze | xargs pip uninstall -y
For Windows Machine or Linux Machine:
First, save all installed packages in a requirement.txt file, using the below command
pip freeze > requirements.txt
Then use the below command to uninstall all packages
pip uninstall -r requirements.txt -y
If the above command may get failed if PIP is not able to uninstall any specific package due to some dependency and may throw error as below
ERROR: Cannot uninstall 'llvmlite'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
Then just edit the requirements.txt file and comment out that specific package and save the file and execute the above again. [Workaround]
To deal above problem we can use the below script to ease the process.
For Windows:
foreach($line in Get-Content requirements.txt) { if(!($line.StartsWith('#'))){ pip uninstall $line -y } }
For Linux:
cat requirements.txt | xargs -n 1 pip uninstall -y