我知道如何使用pip查看已安装的Python包,只需使用pip freeze.但有没有办法看到使用pip安装或更新软件包的日期和时间?
der*_*sse 22
如果不需要在更新和安装之间有所不同,则可以使用包文件的更改时间.
像pip <10的Python 2一样:
import pip, os, time
for package in pip.get_installed_distributions():
print "%s: %s" % (package, time.ctime(os.path.getctime(package.location)))
Run Code Online (Sandbox Code Playgroud)
或者更喜欢新版本(使用Python 3.7测试并安装了setuptools 40.8 pkg_resources):
import pkg_resources, os, time
for package in pkg_resources.working_set:
print("%s: %s" % (package, time.ctime(os.path.getctime(package.location))))
Run Code Online (Sandbox Code Playgroud)
numpy 1.12.1: Tue Feb 12 21:36:37 2019两种情况下的输出都会像.
顺便说一句:代替使用pip freeze你可以使用pip list哪些能够提供更多信息,比如过时的套餐pip list -o.
jof*_*jof 11
不幸的是,python包装使得这有点复杂,因为没有一致的位置列出了包文件或模块目录的放置位置.
这是我提出的最好的:
#!/usr/bin/env python
# Prints when python packages were installed
from __future__ import print_function
from datetime import datetime
import os
import pip
if __name__ == "__main__":
packages = []
for package in pip.get_installed_distributions():
package_name_version = str(package)
try:
module_dir = next(package._get_metadata('top_level.txt'))
package_location = os.path.join(package.location, module_dir)
os.stat(package_location)
except (StopIteration, OSError):
try:
package_location = os.path.join(package.location, package.key)
os.stat(package_location)
except:
package_location = package.location
modification_time = os.path.getctime(package_location)
modification_time = datetime.fromtimestamp(modification_time)
packages.append([
modification_time,
package_name_version
])
for modification_time, package_name_version in sorted(packages):
print("{0} - {1}".format(modification_time,
package_name_version))
Run Code Online (Sandbox Code Playgroud)
解决方案1:packages.date.py:
import os
import time
from pip._internal.utils.misc import get_installed_distributions
for package in get_installed_distributions():
print (package, time.ctime(os.path.getctime(package.location)))
Run Code Online (Sandbox Code Playgroud)
解决方案 2:packages.alt.date.py:
#!/usr/bin/env python
# Prints when python packages were installed
from __future__ import print_function
from datetime import datetime
from pip._internal.utils.misc import get_installed_distributions
import os
if __name__ == "__main__":
packages = []
for package in get_installed_distributions():
package_name_version = str(package)
try:
module_dir = next(package._get_metadata('top_level.txt'))
package_location = os.path.join(package.location, module_dir)
os.stat(package_location)
except (StopIteration, OSError):
try:
package_location = os.path.join(package.location, package.key)
os.stat(package_location)
except:
package_location = package.location
modification_time = os.path.getctime(package_location)
modification_time = datetime.fromtimestamp(modification_time)
packages.append([
modification_time,
package_name_version
])
for modification_time, package_name_version in sorted(packages):
print("{0} - {1}".format(modification_time,
package_name_version))
Run Code Online (Sandbox Code Playgroud)
解决方案 1 和 2 兼容性:
您可以使用以下--log选项:
--log <path> Path to a verbose appending log. This log is inactive by default.
Run Code Online (Sandbox Code Playgroud)
例如:
$ pip install --log ~/.pip/pip.append.log gunicorn
Run Code Online (Sandbox Code Playgroud)
或者您可以将其设置pip.conf为默认启用:
[global]
log = <path>
Run Code Online (Sandbox Code Playgroud)
然后,所有pip操作将连同日志分隔符和时间戳一起详细记录到指定文件中,例如:
$ pip install --log ~/.pip/pip.append.log gunicorn
$ pip install --log ~/.pip/pip.append.log --upgrade gunicorn
Run Code Online (Sandbox Code Playgroud)
将以下内容记录到 ~/.pip/pip.append.log:
------------------------------------------------------------
/usr/bin/pip run on Mon Jul 14 14:35:36 2014
Downloading/unpacking gunicorn
...
Successfully installed gunicorn
Cleaning up...
------------------------------------------------------------
/usr/bin/pip run on Mon Jul 14 14:35:57 2014
Getting page https://pypi.python.org/simple/gunicorn/
URLs to search for versions for gunicorn in /usr/lib/python2.7/site-packages:
* https://pypi.python.org/simple/gunicorn/
...
Requirement already up-to-date: gunicorn in /usr/lib/python2.7/site-packages
Cleaning up...
Run Code Online (Sandbox Code Playgroud)
你可以从这个日志中解析出你需要的内容。虽然不是最好的,但它是标准pip设施。
pip freeze为您提供所有已安装的软件包。假设您知道该文件夹:
time.ctime(os.path.getctime(file))
应该为您提供文件的创建时间,即安装或更新包的日期。
| 归档时间: |
|
| 查看次数: |
15862 次 |
| 最近记录: |