# Install setuptools to be able to download the following
sudo apt-get install python-setuptools# Install statlib for lightweight statistical tools
sudo easy_install statlib# Install construct for packing/unpacking binary data
sudo easy_install construct
I just installed the python modules: construct and statlib with setuptools like this:
# Install setuptools to be able to download the following
sudo apt-get install python-setuptools
# Install statlib for lightweight statistical tools
sudo easy_install statlib
# Install construct for packing/unpacking binary data
sudo easy_install construct
I want to be able to (programmatically) check their versions. Is there an equivalent to python --version I can run from the command line?
For an individual module, you can try the __version__ attribute, however there are modules without it:
$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
Lastly, as the commands in your question are prefixed with sudo, it appears you’re installing to the global python environment. Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper
Use pkg_resources module distributed with setuptools library. Note that the string that you pass to get_distribution method should correspond to the PyPI entry.
Note that the string that you pass to the get_distribution method should be the package name as registered in PyPI, not the module name that you are trying to import.
Unfortunately these aren’t always the same (e.g. you do pip install memcached, but import memcache).
回答 3
我认为这可以帮助您,但请先安装show软件包才能运行,pip show然后使用show查找版本!
sudo pip install show# in order to get package version execute the below command
sudo pip show YOUR_PACKAGE_NAME | grep Version
module.__version__ is a good first thing to try, but it doesn’t always work.
If you don’t want to shell out, and you’re using pip 8 or 9, you can still use pip.get_installed_distributions() to get versions from within Python:
update:the solution here works in pip 8 and 9, but in pip 10 the function has been moved from pip.get_installed_distributions to pip._internal.utils.misc.get_installed_distributions to explicitly indicate that it’s not for external use. It’s not a good idea to rely on it if you’re using 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', ...]
回答 7
先前的答案不能解决我的问题,但是这段代码可以解决:
import sys
for name, module in sorted(sys.modules.items()):if hasattr(module,'__version__'):print name, module.__version__
Since python 3.8 it’s included in the standard library.
Then, to check a package’s version (in this example lxml) run:
>>> from importlib_metadata import version
>>> version('lxml')
'4.3.1'
Keep in mind that this works only for packages installed from PyPI. Also, you must pass a package name as an argument to the version method, rather than a module name that this package provides (although they’re usually the same).
first add python, pip to your environment variables. so that you can execute your commands from command prompt. then simply give python command.
then import the package
–>import scrapy
then print the version name
–>print(scrapy.__version__)
This will definitely work
回答 12
假设我们正在使用Jupyter Notebook(如果使用Terminal,请删除感叹号):
1)如果软件包(例如xgboost)是通过pip安装的:
!pip show xgboost
!pip freeze | grep xgboost
!pip list | grep xgboost
Note 1: We must regard the python version. If we have installed different versions of python, we have to open the terminal in the python version we are interested in. For example, opening the terminal with python3.8 can (surely will) give a different version of a library than opening with python3.5 or python2.7.
Note 2: We avoid using the print function, because its behavior depends on python2 or python3. We do not need it, the terminal will show the value of the expression.
This works in Jupyter Notebook on Windows, too! As long as Jupyter is launched from a bash-compliant command line such as Git Bash (MingW64), the solutions given in many of the answers can be used in Jupyter Notebook on Windows systems with one tiny tweak.
I’m running windows 10 Pro with Python installed via Anaconda, and the following code works when I launch Jupyter via Git Bash (but does not when I launch from the Anaconda prompt).
The tweak: Add an exclamation mark (!) in front of pip to make it !pip.
>>>!pip show lxml | grep Version
Version: 4.1.0
>>>!pip freeze | grep lxml
lxml==4.1.0
>>>!pip list | grep lxml
lxml 4.1.0
>>>!pip show lxml
Name: lxml
Version: 4.1.0
Summary: Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.
Home-page: http://lxml.de/
Author: lxml dev team
Author-email: lxml-dev@lxml.de
License: BSD
Location: c:\users\karls\anaconda2\lib\site-packages
Requires:
Required-by: jupyter-contrib-nbextensions
回答 17
快速的python程序列出所有包装(您可以将其复制到requirements.txt)
from pip._internal.utils.misc import get_installed_distributions
print_log =''for module in sorted(get_installed_distributions(), key=lambda x: x.key):
print_log += module.key +'~='+ module.version +'\n'print(print_log)
I found it quite unreliable to use the various tools available (including the best one pkg_resources mentioned by Jakub Kukul’ answer), as most of them do not cover all cases. For example
built-in modules
modules not installed but just added to the python path (by your IDE for example)
two versions of the same module available (one in python path superseding the one installed)
Since we needed a reliable way to get the version of any package, module or submodule, I ended up writing getversion. It is quite simple to use:
from getversion import get_module_version
import foo
version, details = get_module_version(foo)
Building on Jakub Kukul’s answer I found a more reliable way to solve this problem.
The main problem of that approach is that requires the packages to be installed “conventionally” (and that does not include using pip install --user), or be in the system PATH at Python initialisation.
To get around that you can use pkg_resources.find_distributions(path_to_search). This basically searches for distributions that would be importable if path_to_search was in the system PATH.
We can iterate through this generator like this:
avail_modules = {}
distros = pkg_resources.find_distributions(path_to_search)
for d in distros:
avail_modules[d.key] = d.version
This will return a dictionary having modules as keys and their version as value. This approach can be extended to a lot more than version number.
Thanks to Jakub Kukul for pointing to the right direction