website/content/blog/detectpythonversion.md

25 lines
785 B
Markdown
Raw Normal View History

2021-03-15 22:16:16 +00:00
---
2023-03-10 17:41:04 +00:00
date: 2021-03-15 22:09:38
2021-03-15 22:16:16 +00:00
draft: false
2023-01-05 19:04:45 +00:00
medium_enabled: true
2023-03-10 17:41:04 +00:00
medium_post_id: f742be560b7f
tags:
- Python
title: Detect Python Version
2021-03-15 22:16:16 +00:00
---
I was working on a distribution recently where `python` was mapped to `python2`. It mixed me up for a bit since I was writing a script for `python3` but it ran partially under `python2`. To lower confusion in the future, I think it's a great idea to check the python version and exit if it isn't the version you expect.
```python
from sys import version_info, exit
if version_info.major != 3:
print("This script only supports Python 3")
print("Curent version: " + \
str(version_info.major) + "." + \
str(version_info.minor) + "." + \
str(version_info.micro)
)
print("Exiting...")
exit(1)
2023-03-10 17:41:04 +00:00
```