我使用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
如何从命令行检查它们的版本?
当前回答
总而言之:
conda list
(它将提供所有的库以及版本细节。)
And:
pip show tensorflow
(它提供了完整的图书馆细节。)
其他回答
在搜索了互联网之后,试图弄清楚如何确保我正在运行的模块的版本(显然是python_is_terrible . exe)。__version__在Python 2中不是一个东西?)从字面上看,这些答案都不适用于我的场景……
然后我想了一会儿,意识到基本原理……失败30分钟后…
假设模块已经安装并且可以导入
Python 3.7
>>> import sys,sqlite3
>>> sys.modules.get("sqlite3").version
'2.6.0'
>>> ".".join(str(x) for x in sys.version_info[:3])
'3.7.2'
Python 2.7
>>> import sys,sqlite3
>>> sys.modules.get("sqlite3").version
'2.6.0'
>>> ".".join(str(x) for x in sys.version_info[:3])
'2.7.11'
字面上就是这样…
首先将可执行文件python和pip添加到环境变量中。这样您就可以从命令提示符执行您的命令。然后简单地给出Python命令。
然后导入包:
import scrapy
然后打印版本名
print(scrapy.__version__)
这肯定有用。
这个答案是为Windows用户准备的。正如在所有其他答案中建议的那样,你可以这样使用语句:
import [type the module name]
print(module.__version__) # module + '.' + double underscore + version + double underscore
但是,有一些模块即使在使用上面的方法后也不打印它们的版本。所以,你可以简单地做:
Open the command prompt. Navigate to the file address/directory by using cd (file address) where you've kept your Python and all supporting modules installed. If you have only one Python interpreter on your system, the PyPI packages are normally visible in the directory/folder: Python → Lib → site-packages. use the command "pip install [module name]" and hit Enter. This will show you a message as "Requirement already satisfied: file address\folder name (with version)". See the screenshot below for example: I had to know the version of a pre-installed module named "Selenium-Screenshot". It correctly showed as 1.5.0:
模块。__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', ...]
您可以简单地使用子流程。getoutput (python3——版本):
import subprocess as sp
print(sp.getoutput(python3 --version))
# Or however it suits your needs!
py3_version = sp.getoutput(python3 --version)
def check_version(name, version):...
check_version('python3', py3_version)
有关更多信息和不依赖__version__属性的方法:
指定os的输出。系统的一个变量,并防止它显示在屏幕上
你也可以使用subprocess.check_output(),当子进程返回的不是退出码0时,它会引发一个错误:
subprocess—子进程管理