Skip to content

How to Check Your Python Version: Command Line & Script Methods

Updated on

Python, being one of the most popular programming languages, has a variety of applications, ranging from web development to data science. As a result, managing Python versions becomes essential for ensuring compatibility and efficiency in development. This essay will explore how to check your Python version using the command line and scripts, along with the importance of version control and virtual environments.

Want to quickly create Data Visualization from Python Pandas Dataframe with No code?

PyGWalker is a Python library for Exploratory Data Analysis with Visualization. PyGWalker (opens in a new tab) can simplify your Jupyter Notebook data analysis and data visualization workflow, by turning your pandas dataframe (and polars dataframe) into a tableau-alternative User Interface for visual exploration.

PyGWalker for Data visualization (opens in a new tab)

Why is it important to check the Python version?

Checking the Python version is important for multiple reasons. First, some Python packages and libraries require a specific version of Python to function correctly. For instance, some functions in NumPy or Pandas may only work with certain Python versions. Knowing your Python version can help you avoid compatibility issues and errors while developing.

Second, new Python versions often come with performance improvements, updated syntax, and new features. Upgrading your Python version can lead to more efficient and easier-to-read code. For example, Python 2 and Python 3 have different syntax and features, and you can learn about these differences here.

How to check the Python version on the command line

You can easily check the Python version using the command line in Windows, Mac, or Linux. For instance, if you want to check the Python version on Ubuntu or Linux, you can use the following command:

python --version

Similarly, to check the Python version on Mac, you can use the same command. However, if you have both Python 2 and Python 3 installed on your system, you may need to use python2 or python3 instead of python in the command.

How to check the Python version in a script

You can also check the Python version within a script using the sys or platform modules. The following is an example of a Python version check script using the sys module:

import sys  print("Python version") print(sys.version) print("Version info.") print(sys.version_info)

If you prefer using the platform module, the script would look like this:

import platform  print("Python version") print(platform.python_version())

Both methods will output the Python version as well as additional information, such as the build and compiler details.

Checking the version of a specific package or library

In addition to checking the Python version, you may also want to check the version of specific packages or libraries. This can be done using the pip package manager. For example, to check the version of NumPy, you can use the following command:

pip show numpy

This command will display the package's version, summary, and other details.

How to switch operations depending on whether Python 2 or Python 3 is being used

In some cases, you may need to write a script that works with both Python 2 and Python 3. To accomplish this, you can use conditional statements in your code based on the detected Python version. The following is an example that demonstrates how to switch operations depending on whether Python 2 or Python 3 is being used:

import sys
 
if sys.version_info[0] == 2:
    # Python 2 specific code
    print("This is Python 2")
elif sys.version_info[0] == 3:
    # Python 3 specific code
    print("This is Python 3")
else:
    print("Unknown Python version")

This approach allows you to maintain compatibility with both versions of Python by executing different code blocks based on the detected Python version.

Using virtual environments in Python

Managing multiple Python projects with different dependencies and versions can be challenging. To address this, you can use virtual environments in Python to isolate project dependencies and Python versions.

Virtual environments enable you to maintain separate Python installations for each project, ensuring that packages and versions do not conflict with each other. This is especially useful when working on projects that have different requirements or when collaborating with other developers.

To create a virtual environment, you can use the venv module (Python 3) or virtualenv package (Python 2). Once the virtual environment is activated, you can install packages and manage dependencies independently from your system-wide Python installation.

Best practices for coding in Python

While managing Python versions and dependencies is essential, it is also important to adhere to best practices for coding in Python. Some of these best practices include:

  1. Following the PEP 8 style guide on official Python.org webpage for Python code.
  2. Writing modular, reusable, and maintainable code.
  3. Using descriptive variable and function names.
  4. Writing comprehensive comments and documentation.
  5. Implementing error handling and logging.
  6. Using version control systems, such as Git, for collaboration and code management.

By following these best practices, you can ensure that your Python code is efficient, easy to understand, and maintainable.

Conclusion

In conclusion, checking your Python version is crucial for compatibility, performance, and efficient development. You can check your Python version using the command line or within a script. Furthermore, managing your Python projects with virtual environments can help maintain separate dependencies and versions, while adhering to best practices to ensure efficient and maintainable code.