How to Check If Python Package Is Installed

Published on Aug. 22, 2023, 12:15 p.m.

To check if a Python package is installed, you can use the pip command in the terminal or command prompt. Here are the steps you can follow:

  1. Open the terminal or command prompt.
  2. Type pip list and press Enter. This will list all packages that are installed globally.
  3. If you want to check if a specific package is installed, type pip freeze | grep package_name and replace package_name with the name of the package you want to check. This will search for the specific package in the list of installed packages.

If the package is installed, you will see it listed in the output of the command. If it is not installed, you will not see it in the output.

Note that if you are using a virtual environment, you will need to activate the environment before running the pip commands. You can do this by typing source path/to/venv/bin/activate on Mac/Linux or path/to/venv\Scripts\activate.bat on Windows, where path/to/venv is the path to your virtual environment directory.

Alternatively, you can try to import the package in a Python script and catch the ImportError exception if it is not installed. For example:

try:
    import package_name
except ImportError:
    print("Package not installed.")

check the version of a Python package

You can check the version of a Python package using the command line or in a Python script.

To check the version from the command line, you can use the following command:

pip show package_name

Replace “package_name” with the name of the package you want to check. This command will display information about the package including the version number.

Alternatively, you can check the version of a package from within a Python script. For example, if you want to check the version of the pandas library, you can use the following code:

import pandas as pd

print(pd.__version__)

This will output the current version of the pandas library that is installed.

Tags:

pip