How to check python package version
How to check python package version
8 Best Ways to Check the Package Version in Python
In this article, I’ll show you how to check the version of a Python module (package, library).
These are the eight best ways to check the version of a Python module:
Let’s dive into some examples for each of those next!
Table of Contents
Method 1: pip show
This will work if your pip installation is version 1.3 or higher—which is likely to hold in your case because pip 1.3 was released a decade ago in 2013!!
Here’s an example in my Windows Powershell for NumPy: I’ve highlighted the line that shows that my package version is 1.21.0 :
In some instances, this will not work—depending on your environment. In this case, try those commands before giving up:
Of course, replace “ numpy ” with your particular package name.
Method 2: pip list
To check the versions of all installed packages, use pip list and locate the version of your particular package in the output list of package versions sorted alphabetically.
This will work if your pip installation is version 1.3 or higher.
Here’s an example in my Windows Powershell, I’ve highlighted the line that shows that my package version is 1.21.0:
In some instances, this will not work—depending on your environment. Then try those commands before giving up:
Method 3: pip list + findstr on Windows
To check the versions of a single package on Windows, you can chain pip list with findstr xyz using the CMD or Powershell command: pip3 list | findstr numpy to locate the version of your particular package xyz in the output list of package versions automatically.
Here’s an example for numpy :
Method 4: Library.__version__ Attribute
Here’s an excerpt from the PEP 8 docs mentioning the __version__ attribute.
“PEP 8 describes the use of a module attribute called __version__ for recording “Subversion, CVS, or RCS” version strings using keyword expansion. In the PEP author’s own email archives, the earliest example of the use of an __version__ module attribute by independent module developers dates back to 1995.”
Method 5: importlib.metadata.version
Method 6: conda list
If you have created your Python environment with Anaconda, you can use conda list to list all packages installed in your (virtual) environment. Optionally, you can add a regular expression using the syntax conda list regex to list only packages matching a certain pattern.
How to list all packages in the current environment?
Method 7: pip freeze
The pip freeze command without any option lists all installed Python packages in your environment in alphabetically order (ignoring UPPERCASE or lowercase). You can spot your specific package if it is installed in the environment.
Output from my local Windows environment with PowerShell (strange packages I know) ;):
You can modify or exclude specific packages using the options provided in this screenshot:
Method 8: pip freeze + grep on Linux/Ubuntu/macOS
To check the versions of a single package on Linux/Ubuntu/macOS, you can chain pip freeze with grep xyz using the CMD or Powershell command: pip freeze | grep xyz to programmatically locate the version of your particular package xyz in the output list of package versions.
Here’s an example for numpy :
Related Questions
Check Package Version Python
How to check package version in Python?
Check Package Version Linux
How to check my package version in Linux?
Check Package Version Ubuntu
How to check my package version in Ubuntu?
Check Package Version Windows
How to check package version on Windows?
Check Package Version Mac
How to check package version on macOS?
Check Package Version Jupyter Notebook
How to check package version in your Jupyter Notebook?
For example, this is a screenshot on how this looks for numpy in a Jupyter Notebook:
Check Package Version Terminal
How to check package version in my terminal?
Check Package Version Conda/Anaconda
How to check package version in my conda installation?
Use conda list ‘my_package’ to list version information about the specific package installed in your (virtual) environment.
Check Package Version with PIP
How to check package version with pip?
Check Package Version in VSCode or PyCharm
How to check package version in VSCode or PyCharm?
Integrated Development Environments (IDEs) such as VSCode or PyCharm provide a built-in terminal where you can run pip show my_package to check the current version of my_package in the specific environment you’re running the command in.
You can type any of those commands in your IDE terminal like so:
Summary
In this article, you’ve learned those best ways to check a Python package version:
Thanks for giving us your valued attention — we’re grateful to have you here! 🙂
Programmer Humor
There are only 10 kinds of people in this world: those who know binary and those who don’t.
👩🧔♂️
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
👩🧔♂️👱♀️
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.
How to check if python package is latest version programmatically?
How do you check if a package is at its latest version programmatically in a script and return a true or false?
I can check with a script like this:
or with command line:
But how do I check programmatically and return true or false?
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Fast Version (Checking the package only)
The program then runs pip show package_name and gets the current version of the package.
If it finds a match, it returns True, otherwise False.
This is a reliable option given that it stands on pip
Edit 2021: The code below no longer works with the new version of pip
My project johnnydep has this feature.
To just check the latest version on PyPi (for Python 3.6 and up):
$ pip install requests
Checking Installed version:
One way to check installed version is just to access the __version__ attribute of the top-level namespace:
Unfortunately not all projects set this attribute, it’s just a common convention in Python. When they don’t have a version attribute, you can use importlib.metadata to query the package version. This way does not actually require importing the package itself, since it’s retrieved from the package metadata which gets written out when the package was installed.
This functionality is available since Python 3.8. In older Python versions, you can use pkg_resources similarly, which is a part of setuptools :
Checking Latest version:
There isn’t currently a way to do this within stdlib. But my project luddite has this feature:
This should do the trick at least for demo purposes. Simply call isLatestVersion with the name of the package you would like to check. If you are using this somewhere important you would want to try/catch the url request as internet access may not be available. Also note that if the package is not installed isLatestVersion will return False.
This is tested for Python 3.7.4 and Pip 19.0.3.
Edit: Remove pip search
Thanks for the several suggestions. Here is a new version that doesn’t use pip search but instead pulls the latest version directly from pypi as proposed by Daniel Hill. This also resolves the issue with the substring false matches.
Original Response
Here is a fast solution that retrieves latest version information on only the gekko package of interest.
Checking a Python module version at runtime
Many third-party Python modules have an attribute which holds the version information for the module (usually something like module.VERSION or module.__version__ ), however some do not.
Particular examples of such modules are libxslt and libxml2.
I need to check that the correct version of these modules are being used at runtime. Is there a way to do this?
A potential solution wold be to read in the source at runtime, hash it, and then compare it to the hash of the known version, but that’s nasty.
Is there a better solutions?
7 Answers 7
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Use pkg_resources. Anything installed from PyPI at least should have a version number.
If you’re on python >=3.8 you can use a module from the built-in library for that. To check a package’s version (in this example lxml ) run:
This functionality has been ported to older versions of python ( ) as well, but you need to install a separate library first:
and then to check a package’s version (in this example lxml ) run:
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).
I’d stay away from hashing. The version of libxslt being used might contain some type of patch that doesn’t effect your use of it.
As an alternative, I’d like to suggest that you don’t check at run time (don’t know if that’s a hard requirement or not). For the python stuff I write that has external dependencies (3rd party libraries), I write a script that users can run to check their python install to see if the appropriate versions of modules are installed.
For the modules that don’t have a defined ‘version’ attribute, you can inspect the interfaces it contains (classes and methods) and see if they match the interface they expect. Then in the actual code that you’re working on, assume that the 3rd party modules have the interface you expect.
Check Python Module Version
It is usually recommended to use the pip command to install Python modules. It is because, using the pip command, we can specify the required version of the module which we wish to install.
Modules are updated regularly. New functions and features are added regularly, and some also get deprecated, which may lead to errors if one is not aware of these changes. Thus, it is essential to be in knowledge of what version of the module is installed.
In this tutorial, we will discuss how to check for the version of a module in Python.
Use the __version__() Method to Find the Version of a Module in Python
Usually, most of the modules have the __version__() method associated with them, revealing its version.
Please enable JavaScript
However, it is not advisable to use this method. First off, __version__() is a magic method that is usually not meant to be called explicitly. Secondly, not every module has this attribute that can tell its version.
Use the importlib.metadata Module to Find the Version of a Module in Python
In Python v3.8 and above, we have the importlib.metadata module, which has the version() function. This function will return the version of the specified module.
We can also use the import_metadata module for older versions of Python.
Use the pkg_resources Module to Find the Version of a Module in Python
Below Python 3.8, we can use the get_distribution.version() method from the pkg_resources module to find a module version. Note that the string that you pass to the get_distribution method should correspond to the PyPI entry.
Use the pip show Command to Find the Version of a Module in Python
Alternatively, we can use the pip show command to find out details about a specific package that includes its version.
How to Check ‘dash’ Package Version in Python?
In this article, I’ll show you:
These are the eight best ways to check the installed version of the Python module dash :
Before we go into these ways to check your dash version, let’s first quickly understand how versioning works in Python—you’ll be thankful to have spent a few seconds on this topic, believe me!
Table of Contents
A Note on Python Version Numbering
In this tutorial, we’ll use the shorthand general version abbreviation like so:
This is shorthand for
Let’s dive into the meat of this article:
💬 Question: How to check the (major, minor, patch) version of dash in your current Python environment?
Method 1: pip show
To check which version of the Python library dash is installed, run pip show dash or pip3 show dash in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu).
This will work if your pip installation is version 1.3 or higher—which is likely to hold in your case because pip 1.3 was released a decade ago in 2013!!
Here’s an example in my Windows Powershell: I’ve highlighted the line that shows that my package version is a.b.c :
In some instances, this will not work—depending on your environment. In this case, try those commands before giving up:
Next, we’ll dive into more ways to check your dash version.
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Method 2: pip list
To check the versions of all installed packages, use pip list and locate the version of dash in the output list of package versions sorted alphabetically.
This will work if your pip installation is version 1.3 or higher.
Here’s a simplified example for Windows Powershell, I’ve highlighted the line that shows the package version is 1.2.3 :
In some instances, this will not work—depending on your environment. Then try those commands before giving up:
Method 3: pip list + findstr on Windows
To check the versions of a single package on Windows, you can chain pip list with findstr dash using the CMD or Powershell command: pip3 list | findstr dash to locate the version of dash in the output list of package versions automatically.
Here’s an example for dash :
Method 4: Module __version__ Attribute
Here’s an excerpt from the PEP 8 docs mentioning the __version__ attribute.
“PEP 8 describes the use of a module attribute called __version__ for recording “Subversion, CVS, or RCS” version strings using keyword expansion. In the PEP author’s own email archives, the earliest example of the use of an __version__ module attribute by independent module developers dates back to 1995.”
You can also use the following one-liner snippet to run this from your terminal (macOS, Linux, Ubuntu) or CMD/Powershell (Windows):
However, this method doesn’t work for all libraries, so while simple, I don’t recommend it as a general approach for that reason.
Method 5: importlib.metadata.version
Method 6: conda list
If you have created your Python environment with Anaconda, you can use conda list to list all packages installed in your (virtual) environment. Optionally, you can add a regular expression using the syntax conda list regex to list only packages matching a certain pattern.
How to list all packages in the current environment?
Method 7: pip freeze
The pip freeze command without any option lists all installed Python packages in your environment in alphabetically order (ignoring UPPERCASE or lowercase). You can spot your specific package dash if it is installed in the environment.
Output example (depending on your concrete environment/installation):
You can modify or exclude specific packages using the options provided in this screenshot:
Method 8: pip freeze + grep on Linux/Ubuntu/macOS
To check the versions of a single package on Linux/Ubuntu/macOS, you can chain pip freeze with grep dash using the CMD or Powershell command: pip freeze | grep dash to programmatically locate the version of your particular package dash in the output list of package versions.
Here’s an example for dash :
Related Questions
Check dash Installed Python
How to check if dash is installed in your Python script?
Check dash Version Python
How to check the package version of dash in Python?
Check dash Version Linux
How to check my dash version in Linux?
To check which version of dash is installed, use pip show dash or pip3 show dash in your Linux terminal.
Check dash Version Ubuntu
How to check my dash version in Ubuntu?
To check which version of dash is installed, use pip show dash or pip3 show dash in your Ubuntu terminal.
Check dash Version Windows
How to check my dash version on Windows?
To check which version of dash is installed, use pip show dash or pip3 show dash in your Windows CMD, command line, or PowerShell.
Check dash Version Mac
How to check my dash version on macOS?
To check which version of dash is installed, use pip show dash or pip3 show dash in your macOS terminal.
Check dash Version Jupyter Notebook
How to check my dash version in my Jupyter Notebook?
Output: The following is an example on how this looks for dash in a Jupyter Notebook cell:
Check dash Version Conda/Anaconda
How to check the dash version in my conda installation?
Use conda list ‘dash’ to list version information about the specific package installed in your (virtual) environment.
Check dash Version with PIP
How to check the dash version with pip?
Check Package Version in VSCode or PyCharm
How to check the dash version in VSCode or PyCharm?
Integrated Development Environments (IDEs) such as VSCode or PyCharm provide a built-in terminal where you can run pip show dash to check the current version of dash in the specific environment you’re running the command in.
You can type any of those commands in your IDE terminal like so:
Summary
In this article, you’ve learned those best ways to check a Python package version:
Thanks for giving us your valued attention — we’re grateful to have you here! 🙂
Programmer Humor
There are only 10 kinds of people in this world: those who know binary and those who don’t.
👩🧔♂️
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
👩🧔♂️👱♀️
Related Tutorials
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.
Источники информации:
- http://stackoverflow.com/questions/58648739/how-to-check-if-python-package-is-latest-version-programmatically
- http://stackoverflow.com/questions/710609/checking-a-python-module-version-at-runtime
- http://www.delftstack.com/howto/python/module-version-python/
- http://blog.finxter.com/how-to-check-dash-package-version-in-python/