How to Check NumPy Version Installed in Your Environment
You can check the installed
numpy version by running numpy.__version__ in Python. Alternatively, use pip show numpy in your command line to see the version details.Syntax
To check the NumPy version in Python, use the attribute __version__ from the numpy module.
numpy.__version__: Returns the version string of the installed NumPy package.
python
import numpy print(numpy.__version__)
Output
1.24.3
Example
This example shows how to import NumPy and print its version. It helps confirm which version is currently installed in your Python environment.
python
import numpy version = numpy.__version__ print(f"Installed NumPy version: {version}")
Output
Installed NumPy version: 1.24.3
Common Pitfalls
Sometimes users try to check the version without importing NumPy first, which causes an error. Also, using pip list might show multiple versions if you have several Python environments.
Always ensure you run the version check in the same environment where your code runs.
python
try: print(numpy.__version__) except NameError: print("Error: NumPy is not imported. Please import numpy first.") # Correct way: import numpy print(numpy.__version__)
Output
Error: NumPy is not imported. Please import numpy first.
1.24.3
Quick Reference
| Method | Description | Example Command |
|---|---|---|
| Python attribute | Check version inside Python code | import numpy; print(numpy.__version__) |
| Command line pip | Check version via pip in terminal | pip show numpy |
| Command line pip list | List all installed packages with versions | pip list | grep numpy |
Key Takeaways
Use
numpy.__version__ in Python to get the installed NumPy version.Always import NumPy before checking its version to avoid errors.
You can also check the version from the command line using
pip show numpy.Make sure you check the version in the correct Python environment.
Using
pip list helps see all installed packages and their versions.