How to Check ‘ast’ Package Version in Python? : Chris

How to Check ‘ast’ Package Version in Python?
by: Chris
blow post content copied from  Finxter
click here to view original post


5/5 - (2 votes)

In this article, I’ll show you:

💬 How to check the version of the Python module (package, library) ast? And how to check if ast is installed anyways?

These are the eight best ways to check the installed version of the Python module ast:

  • Method 1: pip show ast
  • Method 2: pip list
  • Method 3: pip list | findstr ast
  • Method 4: library.__version__
  • Method 5: importlib.metadata.version
  • Method 6: conda list
  • Method 7: pip freeze
  • Method 8: pip freeze | grep ast

Before we go into these ways to check your ast 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!

A Note on Python Version Numbering

💡Python versioning adds a unique identifier to different package versions using semantic versioning. Semantic versioning consists of three numerical units of versioning information in the format major.minor.patch.

Python Version Numbering

In this tutorial, we’ll use the shorthand general version abbreviation like so:

x.y.z

Practical examples would use numerical values for x, y, and z:

  • 1.2.3
  • 4.1.4
  • 1.0.0

This is shorthand for

major.minor.patch
  • Major releases (0.1.0 to 1.0.0) are used for the first stable release or “breaking changes”, i.e., major updates that break backward compatibility.
  • Minor releases (0.1.0 to 0.2.0) are used for larger bug fixes and new features that are backward compatible.
  • Patch releases (0.1.0 to 0.1.1) are used for smaller bug fixes that are backward compatible.

Let’s dive into the meat of this article:

💬 Question: How to check the (major, minor, patch) version of ast in your current Python environment?

Method 1: pip show

To check which version of the Python library ast is installed, run pip show ast or pip3 show ast 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:

PS C:\Users\xcent> pip show ast
Name: ast
Version: a.b.c
Summary: ...
Home-page: ...
Author: ...
Author-email: ...
License: ...
Location: ...
Requires: ...
Required-by: ...

In some instances, this will not work—depending on your environment. In this case, try those commands before giving up:

python -m pip show ast
python3 -m pip show ast
py -m pip show ast
pip3 show ast

Next, we’ll dive into more ways to check your ast 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).

Link: https://nostarch.com/pythononeliners

Method 2: pip list

To check the versions of all installed packages, use pip list and locate the version of ast 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:

PS C:\Users\xcent> pip list
Package         Version
--------------- ---------
aaa             1.2.3
...
ast             1.2.3
...
zzz             1.2.3

In some instances, this will not work—depending on your environment. Then try those commands before giving up:

python -m pip list
python3 -m pip list
py -m pip list
pip3 list 

Method 3: pip list + findstr on Windows

To check the versions of a single package on Windows, you can chain pip list with findstr ast using the CMD or Powershell command: pip3 list | findstr ast to locate the version of ast in the output list of package versions automatically.

Here’s an example for ast:

pip3 list | findstr ast

1.2.3

Method 4: Module __version__ Attribute

To check which version is installed of a given library, you can use the library.__version__ attribute after importing the library (package, module) with import library.

Here’s the code:

import my_library
print(my_library.__version__)
# x.y.z for your version output

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):

python3 -c "import my_library; print(my_library.__version__)"

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

The importlib.metadata library provides a general way to check the package version in your Python script via importlib.metadata.version('ast') for library ast. This returns a string representation of the specific version such as 1.2.3 depending on the concrete version in your environment.

Here’s the code:

import importlib.metadata
print(importlib.metadata.version('ast'))
# 1.2.3

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?

conda list

How to list all packages installed into the environment 'xyz'?

conda list -n xyz

Regex: How to list all packages starting with 'ast'?

conda list '^ast'

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 ast if it is installed in the environment.

pip freeze

Output example (depending on your concrete environment/installation):

PS C:\Users\xcent> pip freeze
aaa==1.2.3
...
ast==1.2.3
...
zzz==1.2.3

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 ast using the CMD or Powershell command: pip freeze | grep ast to programmatically locate the version of your particular package ast in the output list of package versions.

Here’s an example for ast:

pip freeze | grep ast
ast==1.2.3

Related Questions

Check ast Installed Python

How to check if ast is installed in your Python script?

To check if ast is installed in your Python script, you can run import ast in your Python shell and surround it by a try/except to catch a potential ModuleNotFoundError.

try:
    import ast
    print("Module ast installed")
except ModuleNotFoundError:
    print("Module ast not installed")

Check ast Version Python

How to check the package version of ast in Python?

To check which version of ast is installed, use pip show ast or pip3 show ast in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu) to obtain the output major.minor.patch.

pip show ast  # or pip3 show ast
# 1.2.3

Check ast Version Linux

How to check my ast version in Linux?

To check which version of ast is installed, use pip show ast or pip3 show ast in your Linux terminal.

pip show ast  # or pip3 show ast
# 1.2.3

Check ast Version Ubuntu

How to check my ast version in Ubuntu?

To check which version of ast is installed, use pip show ast or pip3 show ast in your Ubuntu terminal.

pip show ast  # or pip3 show ast
# 1.2.3

Check ast Version Windows

How to check my ast version on Windows?

To check which version of ast is installed, use pip show ast or pip3 show ast in your Windows CMD, command line, or PowerShell.

pip show ast  # or pip3 show ast
# 1.2.3

Check ast Version Mac

How to check my ast version on macOS?

To check which version of ast is installed, use pip show ast or pip3 show ast in your macOS terminal.

pip show ast  # or pip3 show ast
# 1.2.3

Check ast Version Jupyter Notebook

How to check my ast version in my Jupyter Notebook?

To check which version of ast is installed, add the line !pip show ast to your notebook cell where you want to check. Notice the exclamation mark prefix ! that allows you to run commands in your Python script cell.

!pip show ast

Output: The following is an example on how this looks for ast in a Jupyter Notebook cell:

Package         Version
--------------- ---------
aaa             1.2.3
...
ast             1.2.3
...
zzz             1.2.3

Check ast Version Conda/Anaconda

How to check the ast version in my conda installation?

Use conda list 'ast' to list version information about the specific package installed in your (virtual) environment.

conda list 'ast'

Check ast Version with PIP

How to check the ast version with pip?

You can use multiple commands to check the ast version with PIP such as pip show ast, pip list, pip freeze, and pip list.

pip show ast
pip list
pip freeze
pip list

The former will output the specific version of ast. The remaining will output the version information of all installed packages and you have to locate ast first.

Check Package Version in VSCode or PyCharm

How to check the ast 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 ast to check the current version of ast in the specific environment you’re running the command in.

pip show ast
pip3 show ast

pip list
pip3 list

pip freeze
pip3 freeze

You can type any of those commands in your IDE terminal like so:

pip IDE check package version

Summary

In this article, you’ve learned those best ways to check a Python package version:

  • Method 1: pip show ast
  • Method 2: pip list
  • Method 3: pip list | findstr ast
  • Method 4: library.__version__
  • Method 5: importlib.metadata.version
  • Method 6: conda list
  • Method 7: pip freeze
  • Method 8: pip freeze | grep ast

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


July 17, 2022 at 09:39PM
Click here for more details...

=============================
The original post is available in Finxter by Chris
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce