Python is the third most popular programming language, and it is evolving fast. With frequent updates and shifting compatibility, tracking your Python version is essential. In this guide, we will explore various methods to check the Python version and why choosing the right version is key to secure, stable, and efficient operations.
Python is a widely popular and easy-to-use programming language that is used for a range of applications, such as building websites and software, task automation, data analysis, and data visualization.
Because of its high readability and simplicity, Python is considered quite beginner-friendly and is widely adopted by developers. In fact, a Statista survey revealed that 51% of developers prefer Python over other programming languages, making it the third most popular language globally.
Since its initial public release in 1991 with Python 0.9.0 version, there have been numerous versions of the language, broadly classified into two main series: Python 2.x and Python 3.x.
With its growing popularity and regular updates, it is important to know which version of Python you are using, especially since different projects, libraries, or features may depend on a specific one. Bear in mind that not all versions are created equal, and using the correct version can significantly influence a project's compatibility, performance, and stability.
In this article, we will explore the Python language, including how to check the Python version and its benefits. Let us break it down.
What is the need to check the Python version?
Different Python versions offer various functionalities, syntax, or libraries. In that case, you should be clear that you are using the appropriate version to ensure compatibility, eliminate errors, uphold regulatory compliance, and perform essential troubleshooting tasks. Here is why you are required to check the Python version:
Dependency management: Many Python projects and third-party libraries require specific Python versions. Using an incompatible version may cause errors or failures due to missing features or syntax differences.
Avoiding errors: Code written for a newer Python version may use features not available in older versions. On the other hand, legacy code may not function as expected in newer releases.
Risk mitigation and compliance: Older versions may have unpatched vulnerabilities. Checking which version is installed lets you identify systems that need security updates, critical for regulatory compliance and for ensuring maximum security.
Troubleshooting: When addressing issues on servers or end-user devices, confirming the Python version helps identify root causes of misbehavior or incompatibility.
How to check the Python version?
Understanding how to know the Python version involves familiarity with the command-line interface, built-in Python modules, and sometimes package managers. Here are several effective ways to determine the version of Python installed on your system:
1. Using command line-check for Windows/Linux/MacOS
Open the command prompt by searching for “cmd” in the Start menu. If you are using MacOS, open Terminal by searching “Terminal” in the Spotlight search. In Linux OS, simply press the Ctrl+Alt+T keys to launch the terminal window.
Type the following commands and press Enter: “python --version” or “python -V”
If the above commands do not work or display an error message, try using the “python3--version ” command. This will show the version of Python 3 installed on your system.
The Python version installed on your system is displayed as output.
Note:
On some systems, especially Linux/macOS, you may need to use python3 instead of python.
2. Through Python scripts
This is done by using the following Python programming code:
Import sys
print (f "Python version: {sys. version}")
print (f “Version info: {sys. version_info})
The output provides full details, including the version number, build date, and compiler. It looks like this:
3.10.12 (tags/v3.10.12:7e8fa1b, Jul 8 2023, 04:12:14) [MSC v.1929 64 bit (AMD64)]
Here,
3.10.12: This is the Python version you are using.
Jul 8 2023, 04:12:14: The build date and time of this Python version.
[MSC v.1929 64 bits (AMD64)]: The compiler and architecture info (in this case, Microsoft C compiler, 64-bit, for AMD64 architecture).
3. Using package managers
Package managers are used in the case of managed systems, where they automate the process of installing, updating, and removing software packages on a system. To check the Python version using package managers, you will need to give certain commands. These commands differ across package managers and OS.
On Windows
1. You can use the "Winget" command.
2. You can also check through "Apps & Features" in the system settings of Python.
On Linux
1. For Debian-based systems like Ubuntu, use the command “apt show python3”
2. For Red Hat-based systems, such as CentOS, use the command “yum info python3”
On MacOS
The most common Mac package manager is Homebrew. To see which Python version is installed by Homebrew:
1. Use the “brew list –versions python” command or “brew info python”.
Note:
brew list --versions python may work, but is less commonly used.
4. Checking Python version in Docker
Here is how to check the Python version inside a Docker container:
1. Directly running the command in the container
If the container is already running, use:
docker exec <container-name-or-id> python --version
Use your actual container identifier in place of <container-name-or-id>
2. Starting a new container
If you want to check an image before running it for production, use:
docker run --rm <image-name> python --version
Replace <image-name> with your desired image (e.g., python:3.11)
This command starts a new container from the image, prints the Python version, and removes the container when done
5. Using IDEs
Another way to check the Python version is by using an Integrated Development Environment. These include:
1. PyCharm
Open an existing Python project or create a new one.
Go to “View,” then “Tool Windows,” and from there access the “Terminal."
In that Terminal, type this command: python –version and press Enter.
Output gets displayed in the Terminal window.
2. VS Code
Open a Python file.
Select the Python interpreter visible in the bottom left corner of the editor.
The version will be displayed there.
You can also type the command “python –version” in the Terminal by going to "View" and then "Integrated Terminal."
3. Jupyter Notebook
Open a new notebook
Execute this code:
import sys
print (sys. version)
The output will be the version number
How to check the Python version in virtual environments?
Checking the Python version within a virtual environment is crucial because virtual environments allow you to isolate project dependencies, including the Python interpreter itself. This means different projects on your machine can use different Python versions without conflicts.
When you create a virtual environment using virtualenv, venv, or conda, one of the first things it does is create a copy or symlink to a specific Python interpreter.
This interpreter is then used to run Python scripts.
You can create virtual environments with different versions of Python by specifying which interpreter to use during creation.
This isolation ensures that projects do not interfere with each other’s dependencies or Python versions.
Checking the Python version inside a virtual environment:
1. For venv and virtualenv
Activate the virtual environment by using the following commands:
On macOS/Linux: “source <venv-folder>/bin/activate”
On Windows (Command Prompt): “<venv-folder>\Scripts\activate”
Replace <venv-folder> with your virtual environment directory name
Once activated, check the Python version by running “python –version”
2. For conda environments
Activate the conda environment:
Use the command “conda activate myenv”
Replace "myenv" with your conda environment's name
Check the Python version by running “python –version”
Common Python version issues and how to resolve them
Here are some issues associated with Python versions that you may encounter:
Multiple versions installed: Sometimes, different Python versions are installed on the system. Ensure your scripts explicitly call the correct interpreter path or use version managers to switch between versions.
Legacy versions: When older versions are installed on your system, make sure you upgrade to the supported one. Automation tools can also be used to avoid the task of manually upgrading whenever updates are available.
Command not recognized: This can happen when Python is not installed on the system. The solution is to install Python and ensure it is added to the path if you are using Windows.
Benefits of knowing the correct Python version
Selecting and knowing the correct Python version balances stability, compatibility, security, and performance of your Python applications. Here are the key advantages:
Seamless operations: Knowing the correct version prevents script failures or tool crashes, leading to reduced downtime.
Enhanced security: Knowledge of Python version ensures that the older version is updated with a newer one that includes essential security updates to patch vulnerable gaps.
Quick resolution: Admins need to know the Python version to correctly perform any troubleshooting of environment-specific issues.
Smooth automation: Automation tools and scripts often depend on specific Python versions. Knowing and using the correct version helps avoid runtime errors and incompatibilities.
Easy integrations: By knowing the right Python version, you can prevent integration or compatibility issues that may arise during deployment of tools like Ansible or SaltStack that depend on Python interpreters.
When should you upgrade or downgrade your Python version?
Managing your Python version is not just about always having the latest release; it is about choosing the version that best fits your project’s requirements. While the latest release often offers better performance and security, sometimes sticking with an older version is smarter. Here are some scenarios when you should upgrade, downgrade, or continue using your current version.
Stick with your current Python version when:
Your project runs smoothly without bugs or compatibility issues.
You rely on packages or frameworks that have not been updated yet. Some libraries lag behind the latest Python releases, potentially hindering the smooth functionality of your code if you upgrade.
Older applications might depend on specific Python versions that newer versions do not support; upgrading could break functionality.
Your deployment environment requires a fixed Python version for consistency. Staying with it helps avoid compatibility mismatches.
Upgrade to the latest version:
When the version receives active security patches to protect against security vulnerabilities.
Language improvements, syntax enhancements, and optimizations are released.
When you use certain libraries that drop support for EOL (end of life) Python versions. Upgrading ensures compatibility with newer versions of libraries and frameworks.
If you use modern tools that require recent Python versions and features not present in older versions.
When to downgrade Python version:
Some critical libraries or frameworks your project depends on may not yet support the latest Python releases.
Existing projects created with an older Python version might not work correctly or fail tests with newer Python versions.
Newer Python versions can sometimes introduce bugs or subtle compatibility issues. Reverting to an earlier stable version can be a practical choice.
Final thoughts
For IT professionals, knowing the accurate Python version is critical in ensuring operational stability and efficiency. Deploying automation scripts, integrating third-party tools, or managing multiple environments - all require the right Python version for compatibility, enhanced performance, and security.
Designed for modern IT teams and forward-thinking MSPs, SuperOps ensures your clients never have to compromise on performance or security. With a sleek interface and seamless PSA-RMM integration, SuperOps helps you reduce manual overhead, stay ahead of issues, and scale your business with confidence.
Ready to see SuperOps in action? Schedule a free trial now.
Frequently Asked Questions
1. How to check the Python version on CMD?
To check the Python version in Windows Command Prompt, open CMD and enter “python --version ” or “python -V.” These commands will display the Python version that is installed on your system. Additionally, if you have multiple Python versions installed on your system, you may need to use the “python3 --version” command to display the installed version of Python 3.
2. How do I check all my Python versions?
If you have multiple Python versions installed on your computer, and you want to check them all, follow these steps:
1. For Windows users
-Open Command Prompt or PowerShell and run the command “where python.”
-This command will list all the python.exe paths found in directories listed in your system’s PATH environment variable.
-After finding all Python executables or paths, you can check the version of each separately.
-If you have Python Launcher installed on your system, run the command “py -0”
-This prints all Python versions the launcher can detect.
2. For Linux and MacOS users
-Open a Terminal window and run “ls /usr/bin/python” or “ls /usr/local/bin/python”
-This shows installed Python executables such as python, python2, python2.7, python3, python3.10, etc.
-You can check the version of each Python executable separately.
3. What is a Python version file?
A Python version file is a simple text file that specifies which Python version should be used for a particular project or directory. The file contains a single line with a Python version specification like 3.13.1. It is usually used by Python version managers like pyenv, uv, and others to automatically select and activate the specified Python version whenever you work in that directory.
4. How do I change the Python version?
To change the default Python version, follow these steps:
1. On Windows
-Search "Edit the system environment variables" in Start and open it.
-A “System Properties” dialog box will appear. Click on “Environment Variables.”
-Edit the Path variable in "User variables."
-Move the path of your desired Python version to the top of the list.
-Click OK to save, then start Command Prompt.
-Verify the version using the “python --version” command.
2. On Linux/MacOS
To switch the default Python, you can update the Python symlink to the version you want to use, but this can affect system tools, so it should be used with caution.
3. For All Platforms
Using version managers, like pyenv on UNIX-like systems, or the Windows Python Launcher (py) for Windows, you can easily choose or switch Python versions globally or per project.
5. Can I upgrade my Python version?
Yes, you can upgrade your Python version. The method usually depends on your operating system.
1. On Windows
-Download the latest Python installer from the official Python website.
-Run the installer and select "Add Python to PATH" before installing.
-This upgrades Python to the newest version.
-Alternatively, you can use package managers like Chocolatey or the Microsoft Store.
After upgrading, you can verify your version with the “python --version” command in CMD.
2. On MacOS
-Visit the official Python download page for macOS.
-Download the latest installer.
-Run the installer and follow the prompts.
-Verify installation by running “python3 --version” command in Terminal.
-To upgrade Python using Homebrew, a package manager, open your terminal and run the commands “brew update” and “brew upgrade python.”
3. On Linux
-Update your package list using the command “sudo apt update.”
-Use the command “sudo apt install python3” to install the latest Python version using the apt package manager. Replace `python3` with the version you want to upgrade to.
-Then check with the “python3 --version” command if the Python version is updated.
6. How to download a Python version?
To download a specific Python version, follow these general steps:
1. Go to the official Python download page.
2. Select the latest or desired Python version for your operating system.
3. For Windows or MacOS, download the official installer (e.g., .exe for Windows, .pkg for macOS).
4. Run the installer and follow the setup prompts. On Windows, ensure that you click "Add Python to PATH" during installation.
Many Linux distributions have Python pre-installed. If it’s not installed, you can either download source files from Python.org or install through your package manager. To install through the package manager, follow these steps:
1. Use the appropriate command based on the Linux distribution.
2. For Ubuntu/Debian, use the “sudo apt-get install python3” command.
3. For Fedora, use the “sudo dnf install python3” command.
4. The package manager will download and install the latest version of Python 3
7. How to update the Python version in VS?
To update or change the Python version in Visual Studio Code, follow these steps:
-Install the desired Python version on your system from the official website.
-Open VS Code and your Python project.
-Click on the current Python version shown in the bottom-right status bar of VS Code.
-Select the desired Python version from the list of detected interpreters. If your newly installed version is not listed, you can add it by browsing to the Python executable path manually.
-Restart VS Code to refresh the environment and ensure the new Python version is active.
-Run your code or check the terminal by typing the “python --version” command to confirm the active Python version in VS Code.
8. How to update Python and remove old versions?
To update Python, simply download and install the latest version from the official site or use your system’s package manager (e.g., Homebrew on macOS, apt on Linux). On Windows, run the installer and check "Add Python to PATH." After updating, verify with the “python --version” command.
To remove older Python versions safely, follow these OS-specific tips:
On Windows:
-Open Control Panel and click on” Programs and Features”
-Locate the older Python versions, select each, and click Uninstall.
On MacOS:
-If the version was installed via Homebrew, remove old versions using the “brew uninstall python@<old_version>” command
On Linux:
-Use your package manager to uninstall by using the “sudo apt purge python3.x” command.
9. How to check if Python is installed on Windows?
To check if Python is installed on Windows, open Command Prompt and type “python --version.” If Python is installed and added to your system’s PATH, the command will display the installed Python version. If you get an error saying the command is not recognized, Python is either not installed or not added to the PATH.
10. How to change the Python version in Render?
To change the Python version in Render, simply add a runtime.txt file in the root directory of your project and specify the desired version (e.g., python-3.11.8). Render uses this file during deployment to determine which Python version to use. After adding or updating the file, commit the changes and redeploy your service. If you are using Docker on Render, update the Python version directly in your Dockerfile (e.g., change FROM python:3.10 to FROM python:3.12). Once deployed, you can confirm the version in the build logs.