Sick Gaming
[Tut] How to Check Your Python Version? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Check Your Python Version? (/thread-94069.html)



[Tut] How to Check Your Python Version? - xSicKxBot - 03-16-2020

How to Check Your Python Version?

<div><p><strong>Simple Answer: To check your Python version, run <code>python --version</code> in your command line or shell. </strong></p>
<figure class="wp-block-image"><img src="https://blog.finxter.com/wp-content/uploads/2019/10/grafik-14.png" alt="" class="wp-image-4850" srcset="https://blog.finxter.com/wp-content/uploads/2019/10/grafik-14.png 871w, https://blog.finxter.com/wp-content/uploads/2019/10/grafik-14-300x71.png 300w, https://blog.finxter.com/wp-content/uploads/2019/10/grafik-14-768x182.png 768w" sizes="(max-width: 871px) 100vw, 871px" /></figure>
<p>This general method works across all major operating systems (Windows, Linux, and macOS).</p>
<hr class="wp-block-separator"/>
<p>Do you need to google important Python keywords again and again? Simply download this popular Python cheat sheet, print the high-resolution PDF, and pin it to your office wall: <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<hr class="wp-block-separator"/>
<p>In the following video, I’ll show you how to check your Python version for each operating system (Windows, macOS, Linux, Ubuntu) and programming framework (Jupyter). Or scroll down to read the step-by-step instructions on how to check your Python version. </p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="How to check your Python version? [Windows, Mac, Linux, Jupyter]" width="1400" height="788" src="https://www.youtube.com/embed/7k95szB4iw4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>The Python version output consists of three numbers <strong>major:minor:micro</strong>. For example, version 3.7.2 means that </p>
<ul>
<li>the <strong>major version</strong> is 3, </li>
<li>the <strong>minor version</strong> is 7, and </li>
<li>the <strong>micro version</strong> is 2. </li>
</ul>
<p><strong>[ATTENTION] <em>Different major versions are NOT fully compatible. Different minor versions are compatible. </em></strong></p>
<hr class="wp-block-separator"/>
<p><a rel="noreferrer noopener" aria-label="&quot;Best Python Book on Market&quot; (opens in a new tab)" href="https://blog.finxter.com/coffee-break-python/" target="_blank"><em><strong>“Best Python Book on Market”</strong></em></a></p>
<figure class="wp-block-image size-large"><a href="https://blog.finxter.com/coffee-break-python/" target="_blank" rel="noopener noreferrer"><img src="https://blog.finxter.com/wp-content/uploads/2019/09/CoffeeBreakPython_AmazonTestimonial_JD.jpg" alt=""/></a></figure>
<hr class="wp-block-separator"/>
<p>For example, you can execute code written in Python 3.6.4 in Python 3.7.2 because they are the same major version — Python 3. But you cannot execute code written in Python 2.7.4 in Python 3.7.2 because they are different major versions.</p>
<p>Note that new minor versions can add changes to the language. For example, in Python 3.8 they introduced the <code>reversed()</code> function with dictionaries. You cannot use the reversed() function in older versions of Python. But the vast majority of the language is the same. </p>
<h2 id="win10">Check Python Version Windows 10 (Exact Steps)</h2>
<p>Three steps to check the Python version on your Win 10 operating system:</p>
<ol>
<li><strong>Open the Powershell application</strong>: Press the Windows key to open the start screen. In the search box, type “powershell”. Press enter. </li>
<li><strong>Execute <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="11" data-gr-id="11">command</g></strong>: type <code>python --version</code> and press enter.</li>
<li>The Python version appears in the next line below your command.</li>
</ol>
<h2 id="win7">Check Python Version Windows 7 (Exact Steps)</h2>
<p> Three steps to check the Python version on your Win 7 operating system. </p>
<ol>
<li><strong>Open the command prompt application</strong>: Press the Windows key to open the start screen. In the search box type “command”. Click on the command prompt application. </li>
<li><strong>Execute <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="9" data-gr-id="9">command</g></strong>: type <code>python --version</code> and press enter.</li>
<li>The Python version appears in the next line right below your command.</li>
</ol>
<h2 id="mac">Check Python Version Mac (Exact Steps)</h2>
<p>Four steps to check the Python version on your Mac operating system. </p>
<ol>
<li>Press CMD + Space to open Spotlight.</li>
<li>Type “terminal” and press enter.</li>
<li><strong>Execute <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="11" data-gr-id="11">command</g></strong>: type <code>python --version</code> or <code>python -V</code> and press enter.</li>
<li>The Python version appears in the next line below your command.</li>
</ol>
<h2 id="linux">Check Python Version Linux (Exact Steps)</h2>
<p>Three steps to check the Python version on your Linux operating system. </p>
<ol>
<li><strong>Open the terminal</strong> application (for example, bash).</li>
<li><strong>Execute <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="7" data-gr-id="7">command</g></strong>: type in <code>python --version</code> or <code>python -V</code> and press enter.</li>
<li>The Python version appears in the next line below your command.</li>
</ol>
<h2 id="ubuntu">Check Python Version Ubuntu (Exact Steps)</h2>
<p>Four steps to check the Python version on your Ubuntu operating system. </p>
<ol>
<li><strong>Open Dash</strong>: click the upper left symbol.</li>
<li><strong>Open terminal</strong>: type “terminal”, click on the terminal app.</li>
<li><strong>Execute <g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="10" data-gr-id="10">command</g></strong>: type <code>python --version</code> or <code>python -V</code> and press enter.</li>
<li>The Python version appears in the next line right below your command.</li>
</ol>
<h2 id="jupyter">Check Python Version Jupyter (Exact Steps)</h2>
<p>Three steps to check the Python version in a Jupyter notebook. </p>
<ol>
<li><strong>Open the Jupyter notebook</strong>: type <code><g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling gr-progress" id="4" data-gr-id="4">jupyter</g> notebook</code> in your terminal/console.</li>
<li>Write the following Python code snippet in a code cell:</li>
</ol>
<p><pre data-enlighter-language="python" class="EnlighterJSRAW">
from platform import python_version
print(python_version())
</pre>
</p>
<p> 3. <strong>E</strong>x<strong>ecute </strong>the script.</p>
<p>As an alternative, you can also use the following Python code snippet to check your Python version in a Jupyter notebook:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import sys
>>> sys.version</pre>
<p>Here is a screenshot on my computer:</p>
<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/C_9I-F5fjfkK7qNwzumJTiQkqqCne5Y5JK7hfIJVTYEOJTAOLB9KoHmvp_aCie4BrgRzCw-dlJ7PPJ8oerUb-EgfAgC5BGBOg2GHulKQpR0heXb18p57RBc2y07U8WHnDXXx6pcR" alt=""/></figure>
<h2 id="within-script">How to Check Which Python Version Runs Your Script?</h2>
<p>Sometimes, you want to check Python’s version <strong><em>in your Python program</em></strong>.</p>
<figure class="wp-block-image"><img src="https://blog.finxter.com/wp-content/uploads/2019/10/grafik-15.png" alt="" class="wp-image-4858" srcset="https://blog.finxter.com/wp-content/uploads/2019/10/grafik-15.png 838w, https://blog.finxter.com/wp-content/uploads/2019/10/grafik-15-300x104.png 300w, https://blog.finxter.com/wp-content/uploads/2019/10/grafik-15-768x267.png 768w" sizes="(max-width: 838px) 100vw, 838px" /></figure>
<p>To achieve this, simply import the <a href="https://docs.python.org/3/library/sys.html">sys</a> module and print the <code>sys.version</code> attribute to your Python shell:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import sys
>>> print(sys.version)
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]</pre>
<p> Import the built-in sys module and print sys.version for human-readable output.&nbsp; </p>
<h2 id="versions-all">What are the Different Python Versions?</h2>
<p>Python has three main versions: version 1, version 2, and version 3. Version 4 is currently (2019) under development. </p>
<p>Here is the version history from <a href="https://en.wikipedia.org/wiki/History_of_Python">Wikipedia</a>.</p>
<ul>
<li>Python 0.9.0 – February 20, 1991
<ul>
<li>Python 0.9.1 – February 1991</li>
<li>Python 0.9.2 – Autumn, 1991</li>
<li>Python 0.9.4 – December 24, 1991</li>
<li>Python 0.9.5 – January 2, 1992</li>
<li>Python 0.9.6 – April 6, 1992</li>
<li>Python 0.9.8 – January 9, 1993</li>
<li>Python 0.9.9 – July 29, 1993</li>
</ul>
</li>
</ul>
<ul>
<li>Python 1.0 – January 1994
<ul>
<li>Python 1.2 – April 10, 1995</li>
<li>Python 1.3 – October 12, 1995</li>
<li>Python 1.4 – October 25, 1996</li>
<li>Python 1.5 – December 31, 1997</li>
<li>Python 1.6 – September 5, 2000</li>
</ul>
</li>
<li>Python 2.0 – October 16, 2000
<ul>
<li>Python 2.1 – April 15, 2001</li>
<li>Python 2.2 – December 21, 2001</li>
<li>Python 2.3 – July 29, 2003</li>
<li>Python 2.4 – November 30, 2004</li>
<li>Python 2.5 – September 19, 2006</li>
<li>Python 2.6 – October 1, 2008</li>
<li>Python 2.7 – July 3, 2010</li>
</ul>
</li>
<li>Python 3.0 – December 3, 2008
<ul>
<li>Python 3.1 – June 27, 2009</li>
<li>Python 3.2 – February 20, 2011</li>
<li>Python 3.3 – September 29, 2012</li>
<li>Python 3.4 – March 16, 2014</li>
<li>Python 3.5 – September 13, 2015</li>
<li>Python 3.6 – December 23, 2016</li>
<li>Python 3.7 – June 27, 2018</li>
</ul>
</li>
</ul>
<p>As there are some significant differences in syntax, you should always install the latest version in Python. Keep yourself updated on the official Python website <a href="https://www.python.org/">here</a>.</p>
<h2 id="upgrade-new">How to Upgrade to a Newer Version?</h2>
<p>If you are not using a virtual environment, go to <a href="https://python.org/downloads">python.org/downloads</a> to download and install whatever version you need. It’s the easiest way to upgrade Python.</p>
<p>But now you’ll run into the following problem: how do I run a specific Python version? Check out <a href="https://stackoverflow.com/questions/4583367/how-to-run-multiple-python-versions-on-windows">this</a> StackOverflow answer to learn the exact steps.</p>
<p>Or you can make your life easier by using virtual environments. These let you have multiple versions of Python installed on your system. Plus, you can switch between them instantaneously. One option is to use the built-in module <a href="https://docs.python.org/3/library/venv.html">venv</a>. If you’re a Data Scientist, the industry standard is <a href="https://www.anaconda.com/">Anaconda</a>. </p>
<p>Installing and upgrading different Python versions is easy when you use virtual environments. For a full tutorial of virtual environments, <a href="https://blog.finxter.com/python-virtual-environments-with-venv-a-step-by-step-guide/">read over our introductory Finxter blog article</a>.</p>
<h2>How to Check if Python 3 is Installed?</h2>
<p>If you’ve installed multiple installations of Python, running <code>python --version</code> may give you only the version of Python 2. To check which version of Python 3 is installed on your computer, simply run the command <code>python3 --version</code> instead of <code>python --version</code>. </p>
<h2> How to Check Python Version – Detailed</h2>
<p>Not only does Python have major, minor and micro versions. Each of those versions has further versions, namely the release level and serial. </p>
<p>These are displayed when you run</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0)</pre>
<p>In the above code, I am running Python 3.8.0.</p>
<p>Most of the time, you will only care about the major, minor and micro releases. Release level and serial are usually for the core Python Dev team to work on changes to the language. </p>
<p>The possible release levels are ‘alpha’, ‘beta’, ‘candidate’, or ‘final’. Alpha contains the first updates made to the language. Beta means the language can be tested with some users but still won’t work perfectly. This is where the phrase ‘beta testers’ comes from. A ‘candidate’ has only a few small bugs left to fix. Final is the last version and the one released to the general public. If you want to try out new features before anyone else, you can download these release levels. However, if you just want a version of Python that works, you should choose ‘final’. When you download any version of Python, it will be a ‘final’ release unless stated otherwise. </p>
<p>Serial is for the smallest changes. The Python Dev team increments it as they make changes to the alpha, beta and candidate versions. All final versions have serial=0. They add future changes to the next major/minor/micro releases.</p>
<h2>
How to Make Sure My Script Runs a Specific Python Version?</h2>
<p>Let’s say you’ve just installed Python 3.8. Your script, my_file.py, uses a brand new feature: reversed() when iterating over a dictionary. For other people to run this script, they must also run Python 3.8. So you should put a check at the start to let other users know this.</p>
<p>We do this by adding an assert statement at the top of my_file.py</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># my_file.py
import sys
assert sys.version_info >= (3, 8) my_dict = dict(a=1, b=2, c=3)
for key in reversed(my_dict): print(key)</pre>
<p>The assert statement raises an AssertionError if the statement is False. If the statement is True, the script continues to run. </p>
<p>For example, if I am running Python 3.7 and execute my_file.py from the terminal, this happens</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Running Python 3.7
$ python my_file.py
Traceback (most recent call last): File "my_file.py", line 10, in &lt;module> assert sys.version_info >= (3,8)
AssertionError</pre>
<p>But if I am running Python 3.8, the assert statement does not raise an error, and it executes the rest of the script. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Running Python 3.8
$ python my_file.py
c
b
a</pre>
<p>Note: I have used the <a href="https://www.anaconda.com/">Anaconda</a> virtual environment to install and quickly switch between multiple Python versions. </p>
<h2>Where to Go From Here?</h2>
<p>In summary, you can check the Python version by typing <code>python --version</code> in your operating system shell or command line.</p>
<hr class="wp-block-separator"/>
<p><strong>Do you struggle with Python? And Python source code sometimes feels like a closed book to you? </strong></p>
<p>If this applies to you, check out my <a href="https://blog.finxter.com/subscribe">free email course</a>! The course guides you step-by-step to a deeper and deeper Python level of code understanding. Here is what my readers say:</p>
<blockquote class="wp-block-quote">
<p>Thank you for your newsletter. I find these newsletters highly informative.</p>
<p><cite>Collen</cite></p></blockquote>
<blockquote class="wp-block-quote">
<p>As a fellow educator and fellow (former) Ph.D. student, I just wanted to let you know that I’m really impressed with your teaching materials. You’re doing a really good job!</p>
<p><cite>Daniel</cite></p></blockquote>
<div class="wp-block-button aligncenter is-style-default"><a class="wp-block-button__link has-background has-vivid-red-background-color" href="https://blog.finxter.com/subscribe">Join Us – 100% Free &gt;</a></div>
</div>


https://www.sickgaming.net/blog/2020/03/12/how-to-check-your-python-version/