我正在寻找一些方法来获取virtualenv中的python包的安装日期,但不使用python脚本。我不知道是否有像freeze
这样的工具给我这个信息(至少)。
有什么想法吗?谢谢!
答案 0 :(得分:2)
我们可以使用subprocess,pip show 和时间
来执行此操作import subprocess
import time
import os
package_name = 'requests'
package_param = subprocess.check_output(
['pip', 'show', package_name], stderr=subprocess.STDOUT)
# subprocess will return a string of type `bytes` with
# name, version, location, and we need to get Location, like so:
# Name: requests
# Version: 1.4.5
# another params
# Location: /my/env/lib/python2.7/site-packages
# here we decode pack, because this is a bytes, and spltit by '\n'
# go through this list of strings and check if we have `Location` in string
# if we have location we get this location and with `oc.path.getctime()`
# get the time when this package was created
for param in package_param.decode().split('\n'):
if 'Location' in param:
loc = param.split(':')[1].strip()
print("{}: {}".format(package_name, time.ctime(os.path.getctime(loc))))
<强>输出强>
requests: Fri Jun 15 10:03:50 2018
答案 1 :(得分:0)
实现相同目的的一种方法是检查 site-packages 中的包目录修改日期。
如果您使用的是linux环境,那么您可以运行以下命令:
检查活动的python环境目录
$ which python
样本输出: /[path-to-virtualenv]/bin/python
以格式化日期
列出所有包$ cd /[path-to-virtualenv]/lib/python-[version-number]/site-packages
$ ls -al --time-style="+%Y-%m-%d"
样本输出: drwxrwxr-x 19 [user] [user] [file size] [YEAR-MONTH-DATE] [package name]
要过滤具有特定日期的包,您可以使用grep:
$ ls -al --time-style="+%Y-%m-%d" | grep '2021-03-14'
预期输出: 它将列出所有在 2020-03-14 修改的目录/文件