不使用virtualenv时,查看指定lib目录过时pip包及维护方法咨询
Great question! Let's walk through how to handle this, from direct pip solutions to best practices for maintaining external package libraries.
Direct Pip Solution (No Extra Tools Needed)
You don't need virtualenv for this—pip actually has a built-in way to target a specific directory with the --path flag. Combine it with the --outdated (or -o) option to list packages in your lib directory that have newer versions available:
pip list --outdated --path lib
This will scan the lib directory and output all outdated packages, showing their current version and the latest available version. If you want a cleaner, column-formatted output (the default, but explicit is better), add --format=columns:
pip list --outdated --path lib --format=columns
Alternative: Third-Party Tools for Enhanced Functionality
If you want more features like interactive updates or clearer visualizations, tools like pip-check work well with custom paths. First install it (you can even install it to a temporary directory to avoid affecting your global environment):
pip install pip-check -t temp_lib
Then run it against your lib directory:
python -m pip_check --path lib
It’ll highlight outdated packages and let you choose which ones to update directly.
Best Practices for Maintaining External Package Libraries
If you’re looking for a more structured approach to keep your lib directory in shape, here are some reliable methods:
- Track versions with a locked requirements file: Instead of installing packages ad-hoc to
lib, maintain arequirements.txtwith exact version numbers. Install usingpip install -r requirements.txt -t lib, and periodically update the file to match the actual state oflibwith:
This ensures you always have a record of exactly what’s installed.pip list --path lib --format=freeze > requirements.txt - Full refresh with compatibility testing: Every so often, clear out the
libdirectory entirely, then reinstall from yourrequirements.txt(updating versions first if needed). This ensures all packages are up-to-date and compatible with each other—just be sure to test your application thoroughly afterward! - Use
pip-toolsfor dependency management:pip-toolshelps automate dependency resolution. Usepip-compileto generate a lockedrequirements.txtfrom a looserequirements.infile, andpip-upgradeto update packages while keeping dependencies in check. Combine this withpip install -t libto install directly to your target directory for a streamlined workflow.
内容的提问来源于stack exchange,提问作者yoonjesung

