- How to Get the Version of a Python Library: A Comprehensive Guide
- Using the __version__ attribute
- Using the command line
- Using pkg_resources
- Other package managers
- Using the jantman/versionfinder package
- Other simple code samples to get the version of a Python library
- Conclusion
- Check the version of Python package/library
- Get package version in Python script: __version__ attribute
- Check package version with pip command: pip list , pip freeze , pip show
- List installed packages: pip list
- List installed packages: pip freeze
- Check details of installed packages: pip show
- Check package version with conda command: conda list
- Python: Check Package Version
- Check Version of Python Package
- Python 3: Как узнать версию библиотеки Pandas, Numpy
- Вариант 1. Узнаем версию библиотеки в скрипте Python
- Вариант 2. Проверить с помощью pip менеджера пакетов
- pip list
- pip freeze
- pip show
- Anaconda — conda list
How to Get the Version of a Python Library: A Comprehensive Guide
Learn how to get the version of a Python library or package using different methods, including the __version__ attribute, pip, pkg_resources, and other package managers. Keep your projects up-to-date with the latest versions and easily debug compatibility issues.
- Using the __version__ attribute
- Using the command line
- Using pkg_resources
- Other package managers
- Using the jantman/versionfinder package
- Other simple code samples to get the version of a Python library
- Conclusion
- How do I find my Python library version?
- What is __ version __ in Python?
- How to see Python version in cmd?
- How to check installed Python libraries in cmd?
Python is a popular programming language widely used for various purposes, including web development, machine learning, and data analysis. When working with Python, it is essential to know how to get the version of a Python library or package. In this blog post, we will discuss different ways to get the version of a Python library, including using the __version__ attribute, pip, pkg_resources, and other package managers.
Using the __version__ attribute
The __version__ attribute is a module attribute used for recording version strings. It is often used to check the version of a package. To get the version of a Python package, use the __version__ attribute or import the package and use .__version__ to print the package number.
import requestsprint(requests.__version__)
Using the command line
Another way to check the version of a Python library is to use the command line. You can get the Python version on the command line and in script by executing the python or python3 command with the –version or -V option.
To check the version of a Python library, use pip show library_name or pip3 show library_name. For example:
Name: requests Version: 2.26.0
To check the versions of all installed packages, use the pip list command.
Package Version --------------- ------- requests 2.26.0 .
Using pkg_resources
You can also use pkg_resources to find the version of a Python module at runtime. Use the following code to get the version of a package:
import pkg_resourcespkg_resources.get_distribution("package_name").version
Advantages of using pkg_resources to check the version of a Python module include not having to hardcode version numbers and the ability to check the version of any Python package.
Disadvantages of using pkg_resources include the possibility of multiple installed versions and the need for extra packages/modules.
Other package managers
Several package managers can be used to list installed Python packages , including Pip, Pipenv, Anaconda Navigator, and Conda Package Managers . These package managers have advantages and disadvantages.
Advantages of using these package managers include the ability to manage package dependencies and create virtual environments.
Disadvantages include the need for additional setup and configuration. For example, to install and use Pipenv, you need to install it first:
Then, you can use it to list installed packages like this:
Using the jantman/versionfinder package
The jantman/versionfinder Python package is another way to find the version of a package. You can install the package using pip or download it from Github.
pip install versionfinder
Advantages of using the jantman/versionfinder package include its ability to find the version of a package installed via pip, setuptools, or git.
Other simple code samples to get the version of a Python library
In Python , for instance, how to find the version of python command linw code sample
In Python , for instance, how to check current version of library in python
#Let's say you want to check tensorflow versionimport tensorflow as tf tf.__version__
In Python , for instance, how to know the version of python code example
Step 1: Type Command Prompt on search icon near the windows icon Step 2: On Command Prompt Execute the below command Step 3: python --version
Conclusion
Checking the version of a Python library or package is important for debugging and compatibility issues. Different methods can be used to get the version of a Python package, including using the __version__ attribute, pip, pkg_resources, and other package managers.
Best practices for checking the version of a Python library or package include keeping track of the package versions and updating them regularly. Python cheatsheets and tips for using pip can also be helpful for quickly referencing different Python commands and functions.
By following the methods discussed in this blog post, users can easily get the version of a Python library or package and keep their projects up-to-date with the latest versions.
Check the version of Python package/library
This article explains how to check the versions of packages (libraries) and modules used in Python scripts, and the versions of packages installed in your environment.
If you want to check the version of Python itself, see the following article.
Get package version in Python script: __version__ attribute
To get the version of a package used in a Python script, use the __version__ attribute.
import pandas as pd print(pd.__version__) # 2.0.1
The __version__ attribute, recommended by Python Enhancement Proposals (PEP), is available in many packages.
Note that not all packages have the __version__ attribute because it is not mandatory.
In addition to the __version__ attribute, some packages, such as NumPy and pandas, provide functions and attributes that display more detailed information.
Note that the __version__ is not set for the standard library modules such as math and os . Modules in the standard library do not have individual versions but follow the Python version.
Check package version with pip command: pip list , pip freeze , pip show
If you are using the Python package management system, pip , you can check the information about the installed packages using the following commands. Run these commands in the command prompt or terminal.
In some environments, use pip3 instead of pip . In some cases, pip is for Python2, and pip3 is for Python3.
For basic information on how to use pip, such as installing, updating, and uninstalling packages, please see the following article.
List installed packages: pip list
pip list displays a list of installed package names and version numbers.
$ pip list Package Version ------------------ --------- absl-py 0.1.10 agate 1.6.0 agate-dbf 0.2.0 agate-excel 0.2.1 agate-sql 0.5.2 appnope 0.1.0 .
The pip list command also accepts the following options:
- —format
- Set the display format ( columns , freeze , json )
- Show only out-of-date packages
- Show only the latest packages
Refer to the following article for more information.
List installed packages: pip freeze
pip freeze displays a list of installed package names and version numbers in freeze format.
$ pip freeze absl-py==0.1.10 agate==1.6.0 agate-dbf==0.2.0 agate-excel==0.2.1 agate-sql==0.5.2 appnope==0.1.0 .
The difference between pip freeze and pip list —format freeze is that pip freeze does not show the following package management tools by default.
You can display them all by adding the —all option.
By saving the output in freeze format to a text file, you can install packages in a specified version all at once.
In such cases, listing package management tools like pip isn’t necessary, hence pip freeze omits them by default.
Check details of installed packages: pip show
pip show displays detailed information about a given package.
In addition to version information, detailed information such as license and dependency packages is displayed.
$ pip show pandas Name: pandas Version: 2.0.1 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: Author: Author-email: The Pandas Development Team License: BSD 3-Clause License . Location: /opt/homebrew/lib/python3.11/site-packages Requires: numpy, numpy, python-dateutil, pytz, tzdata Required-by:
Check package version with conda command: conda list
If you have built a Python environment with Anaconda, conda list will list the packages installed in the current virtual environment.
When the environment is not activated, use conda list -n .
Python: Check Package Version
While working with Python you may wonder what is the version of a certain Python package (library) that you are using.
There are several ways of how to get the version of a package or module in Python.
This note describes how to check the version of Python packages from a Python shell, from a command-line (terminal) or using a pip command.
Cool Tip: How to list all the locally installed Python modules and find the paths to their source files! Read More →
Check Version of Python Package
You can get the version of a Python package by running the command as follows directly from a Python shell:
$ python Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pkg_resources; print(pkg_resources.get_distribution('selenium').version) - sample output - 4.3.0
To check the Python package version you can also run the above command from a command-line (terminal) as follows:
$ python -c "import pkg_resources; \ print(pkg_resources.get_distribution('selenium').version)" - sample output - 4.3.0
To get the Python package version using a pip , execute:
$ pip show selenium - sample output - Name: selenium Version: 4.3.0 Summary: None Home-page: https://www.selenium.dev Author: None Author-email: None License: Apache 2.0 Location: /projects/myApp/venv/lib/python3.8/site-packages Requires: trio, trio-websocket, urllib3 Required-by:
Cool Tip: How to list dependencies of the installed Python packages! Read More →
You can also check the Python package version using the pip as follows:
$ pip freeze | grep selenium - sample output - selenium==4.3.0
Python 3: Как узнать версию библиотеки Pandas, Numpy
Вариант 1. Узнаем версию библиотеки в скрипте Python
Для того, чтобы узнать версию библиотеки, необходимо вбить следующую команду (например для Pandas):
import pandas as pd print (pd.__version__)
Пример для Numpy:
import numpy as np print (np.__version__)
Вариант 2. Проверить с помощью pip менеджера пакетов
С помощью менеджера пакетов pip можно проверить версию установленных библиотек, для этого используются команды:
pip list
Выведет список установленных пакетов, включая редактируемые.
pip freeze
Выводит установленные пакеты, которые ВЫ установили с помощью команды pip (или pipenv при ее использовании) в формате требований.
Вы можете запустить: pip freeze > requirements.txt на одной машине, а затем на другой машине (в чистой среде) произвести инсталляцию пакетов: pip install -r requirements.txt .
Таким образом вы получите идентичную среду с точно такими же установленными зависимостями, как и в исходной среде, в которой вы сгенерировал файл requirements.txt.
pip show
Выводит информацию об одном или нескольких установленных пакетах.
Anaconda — conda list
Если вы используете Anaconda, то вы можете проверить список установленных пакетов в активной среде с помощью команды conda list .