Linux python package version

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 .

    Источник

    How To Install Specific Version Of Python Package with Pip?

    How To Install Specific Version Of Python Package with Pip?

    Pip is the most popular tool and the command used to install 3rd party packages into Python. pip can be used for both PYython2 and Python3. In this tutorial, we will learn how to install a specific version of a Python package with the pip command.

    Search Package

    Before installing a specific version of the Python package we can search the package for its complete name and version information. We will use search command in this example.

    Search Package

    Show Package Information and Version

    We can show the Python package information like name, version, etc. The inversion information is important because we will install a specific version or downgrade the Python package.

    Show Package Information and Version

    Install Specific Package Version with pip Command

    We will install a specific version of the Django Python package with pip. We will specify the version we want to install with the equal sign. In this example, we will install the Django version 2.2.1 which is not recent. We will use a double equation sign to specify the version. We will also use the —user option in order to prevent other system users package downgrade. This will only affect the current user.

    $ pip3 install --user django==2.2.1

    Install Specific Package Version with pip Command

    From the screenshot, we see that the currently installed recent Django version is uninstalled automatically.

    Install Package Version Which Is In Specified Range with pip Command

    In the previous example, we have installed a specific django version. But we can also specify the version range with the >= or <= . This will select the latest version which complies with the given expression and install it.

    Install Package Version Which Is In Specified Range with pip Command

    Force Installation of Specific Package Version with pip Command

    If there are some minor problems related to the installation, dependency, etc. We can force the installation with the —force-reinstall option.

    $ pip3 install --force-reinstall --user django==2.2.1

    Remove Specific Package Version with pip Command

    We can also remove the specific package version with the pip command. There is a different alternative. As only a single version can be installed at the same time different version installation will uninstall currently installed version.

    $ pip uninstall django $ pip2 uninstall django $ pip2 uninstall django

    Источник

    Find which version of package is installed with pip

    Using pip, is it possible to figure out which version of a package is currently installed? I know about pip install XYZ —upgrade but I am wondering if there is anything like pip info XYZ . If not what would be the best way to tell what version I am currently using.

    16 Answers 16

    As of pip 1.3, there is a pip show command.

    $ pip show Jinja2 --- Name: Jinja2 Version: 2.7.3 Location: /path/to/virtualenv/lib/python2.7/site-packages Requires: markupsafe 

    In older versions, pip freeze and grep should do the job nicely.

    $ pip freeze | grep Jinja2 Jinja2==2.7.3 

    The naming of ‘freeze’ is historical — it dates back to at least 2008. At the time, many people were already familiar with «freezing» ruby gems, so pip borrowed the then-widely-used name.

    this was not obvious nor documented, but you can type pip show pip to get pip’s version info, rather than pip —version as I would’ve expected.

    I would suggest calling with -i flag as you sometimes do not know which ones are capital letters and which ones are not: pip freeze | grep -i xyz

    I just sent a pull request in pip with the enhancement Hugo Tavares said:

    $ pip show specloud Package: specloud Version: 0.4.4 Requires: nose figleaf pinocchio 

    Pip 1.3 now also has a list command:

    $ pip list argparse (1.2.1) pip (1.5.1) setuptools (2.1) wsgiref (0.1.2) 

    Say package name is X. To find the version — What is the difference between using pip list and doing import X and then X.__version__? Are both the package versions?

    both are valid, but pip list is generic, and __version__ is not. I have also seen version() and get_version() for the imported one.

    and with —outdated as an extra argument, you will get the Current and Latest versions of the packages you are using :

    $ pip list --outdated distribute (Current: 0.6.34 Latest: 0.7.3) django-bootstrap3 (Current: 1.1.0 Latest: 4.3.0) Django (Current: 1.5.4 Latest: 1.6.4) Jinja2 (Current: 2.6 Latest: 2.8) 

    So combining with AdamKG ‘s answer :

    $ pip list --outdated | grep Jinja2 Jinja2 (Current: 2.6 Latest: 2.8) 

    An interesting option. I’d rather have it list all of them, and let me know if any of them are outdated, though.

    The python function returning just the package version in a machine-readable format:

    from importlib.metadata import version version('numpy') 
    pip install importlib-metadata from importlib_metadata import version version('numpy') 

    The bash equivalent (here also invoked from python) would be much more complex (but more robust — see caution below):

    import subprocess def get_installed_ver(pkg_name): bash_str="pip freeze | grep -w %s= | awk -F '==' | tr -d '\n'" %(pkg_name) return(subprocess.check_output(bash_str, shell=True).decode()) 
    # pkg_name="xgboost" # pkg_name="Flask" # pkg_name="Flask-Caching" pkg_name="scikit-learn" print(get_installed_ver(pkg_name)) >>> 0.22 

    Note that in both cases pkg_name parameter should contain package name in the format as returned by pip freeze and not as used during import , e.g. scikit-learn not sklearn or Flask-Caching , not flask_caching .

    Note that while invoking pip freeze in bash version may seem inefficient, only this method proves to be sufficiently robust to package naming peculiarities and inconsistencies (e.g. underscores vs dashes, small vs large caps, and abbreviations such as sklearn vs scikit-learn ).

    Caution: in complex environments both variants can return surprise version numbers, inconsistent with what you can actually get during import .

    One such problem arises when there are other versions of the package hidden in a user site-packages subfolder. As an illustration of the perils of using version() here’s a situation I encountered:

    $ pip freeze | grep lightgbm lightgbm==2.3.1 and $ python -c "import lightgbm; print(lightgbm.__version__)" 2.3.1 vs. $ python -c "from importlib_metadata import version; print(version(\"lightgbm\"))" 2.2.3 until you delete the subfolder with the old version (here 2.2.3) from the user folder (only one would normally be preserved by `pip` - the one installed as last with the `--user` switch): $ ls /home/jovyan/.local/lib/python3.7/site-packages/lightgbm* /home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.2.3.dist-info /home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.3.1.dist-info 

    Another problem is having some conda-installed packages in the same environment. If they share dependencies with your pip-installed packages, and versions of these dependencies differ, you may get downgrades of your pip-installed dependencies.

    To illustrate, the latest version of numpy available in PyPI on 04-01-2020 was 1.18.0, while at the same time Anaconda’s conda-forge channel had only 1.17.3 version on numpy as their latest. So when you installed a basemap package with conda (as second), your previously pip-installed numpy would get downgraded by conda to 1.17.3, and version 1.18.0 would become unavailable to the import function. In this case version() would be right, and pip freeze / conda list wrong:

    $ python -c "from importlib_metadata import version; print(version(\"numpy\"))" 1.17.3 $ python -c "import numpy; print(numpy.__version__)" 1.17.3 $ pip freeze | grep numpy numpy==1.18.0 $ conda list | grep numpy numpy 1.18.0 pypi_0 pypi 

    Источник

    Читайте также:  Convert gpt to mbr with linux
Оцените статью
Adblock
detector