我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
当前回答
根据Jakub Kukul的回答,我找到了一个更可靠的方法来解决这个问题。
这种方法的主要问题是要求包按“常规”安装(不包括使用pip install——user),或者在Python初始化时位于系统PATH中。
要解决这个问题,可以使用pkg_resources. find_distributors (path_to_search)。如果path_to_search在系统PATH中,它基本上会搜索可导入的分布。
我们可以像这样遍历这个生成器:
avail_modules = {}
distros = pkg_resources.find_distributions(path_to_search)
for d in distros:
avail_modules[d.key] = d.version
这将返回一个以模块为键,其版本为值的字典。这种方法可以扩展到比版本号更多的版本。
感谢Jakub Kukul指出了正确的方向。
其他回答
首先将可执行文件python和pip添加到环境变量中。这样您就可以从命令提示符执行您的命令。然后简单地给出Python命令。
然后导入包:
import scrapy
然后打印版本名
print(scrapy.__version__)
这肯定有用。
模块。__version__是一个很好的尝试,但它并不总是有效。
如果你不想退出,并且你正在使用pip 8或9,你仍然可以使用pip.get_installed_distribution()从Python内部获取版本:
这里的解决方案适用于pip 8和9,但在pip 10中,该函数已从pip中移动。get_installed_distribution到pip._internal.utils.misc。get_installed_distribution显式地指出它不供外部使用。如果您正在使用pip 10+,那么依赖它不是一个好主意。
import pip
pip.get_installed_distributions() # -> [distribute 0.6.16 (...), ...]
[
pkg.key + ': ' + pkg.version
for pkg in pip.get_installed_distributions()
if pkg.key in ['setuptools', 'statlib', 'construct']
] # -> nicely filtered list of ['setuptools: 3.3', ...]
总而言之:
conda list
(它将提供所有的库以及版本细节。)
And:
pip show tensorflow
(它提供了完整的图书馆细节。)
如果您的生产系统被强化到无法理解的地步,所以它既没有pip也没有conda,这里有一个Bash替代pip冻结:
ls /usr/local/lib/python3.8/dist-packages | grep info | awk -F "-" '{print $1"=="$2}' | sed 's/.dist//g'
(请确保将dist-packages文件夹更新到当前的python版本,并忽略不一致的名称,例如,下划线vs.破折号)。
样本打印:
Flask==1.1.2
Flask_Caching==1.10.1
gunicorn==20.1.0
[..]
使用pip show查找版本!
# In order to get the package version, execute the below command
pip show YOUR_PACKAGE_NAME | grep Version
您可以使用pip show YOUR_PACKAGE_NAME -它会提供包的所有细节。这也适用于Windows。
grep Version在Linux中用于过滤出版本并显示它。