To check the version of one's Python's Software version, one should use the following code in command prompt:
python -V
Reference: http://docs.python.org/using/cmdline.html#generic-options
Answer from theglauber on Stack OverflowWhat's the most used python version in research?
setuptools - Standard way to embed version into Python package? - Stack Overflow
Which version of python should I download?
Please make `package.__version__` go away! - Packaging - Discussions on Python.org
What is the latest Python version?
What is a Python version file?
How do I change the Python version?
Videos
To check the version of one's Python's Software version, one should use the following code in command prompt:
python -V
Reference: http://docs.python.org/using/cmdline.html#generic-options
In a Python IDE, just copy and paste in the following code and run it (the version will come up in the output area):
import sys
print(sys.version)
I'm writing a program for research and I heard making a virtual environment is a good practice, and since some packages have issue with python versions (I've encountered that only once on a very niche library) I'm wondering which version would be the "best practice" to use
Not directly an answer to your question, but you should consider naming it __version__, not version.
This is almost a quasi-standard. Many modules in the standard library use __version__, and this is also used in lots of 3rd-party modules, so it's the quasi-standard.
Usually, __version__ is a string, but sometimes it's also a float or tuple.
As mentioned by S.Lott (Thank you!), PEP 8 says it explicitly:
Module Level Dunder Names
Module level "dunders" (i.e. names with two leading and two trailing underscores) such as
__all__,__author__,__version__, etc. should be placed after the module docstring but before any import statements except from__future__imports.
You should also make sure that the version number conforms to the format described in PEP 440 (PEP 386 a previous version of this standard).
I use a single _version.py file as the "once cannonical place" to store version information:
It provides a
__version__attribute.It provides the standard metadata version. Therefore it will be detected by
pkg_resourcesor other tools that parse the package metadata (EGG-INFO and/or PKG-INFO, PEP 0345).It doesn't import your package (or anything else) when building your package, which can cause problems in some situations. (See the comments below about what problems this can cause.)
There is only one place that the version number is written down, so there is only one place to change it when the version number changes, and there is less chance of inconsistent versions.
Here is how it works: the "one canonical place" to store the version number is a .py file, named "_version.py" which is in your Python package, for example in myniftyapp/_version.py. This file is a Python module, but your setup.py doesn't import it! (That would defeat feature 3.) Instead your setup.py knows that the contents of this file is very simple, something like:
Copy__version__ = "3.6.5"
And so your setup.py opens the file and parses it, with code like:
Copyimport re
VERSIONFILE="myniftyapp/_version.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = '\"['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
verstr = mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))
Then your setup.py passes that string as the value of the "version" argument to setup(), thus satisfying feature 2.
To satisfy feature 1, you can have your package (at run-time, not at setup time!) import the _version file from myniftyapp/__init__.py like this:
Copyfrom _version import __version__
Here is an example of this technique that I've been using for years.
The code in that example is a bit more complicated, but the simplified example that I wrote into this comment should be a complete implementation.
Here is example code of importing the version.
If you see anything wrong with this approach, please let me know.
I want to program, and what is the best version for me like 3.8, 3.9, or something?
which versioni of Python are you using or considered to be the best one ?
Hey Guys,
I'm looking at random python projects on github. I'm confused as to what the version.py file is for? Why is the file named this? I thought double under syntax was for functions inside your code only? Thanks.
Example - https://github.com/tachang/pygeocoder/blob/master/version.py
It's for storing the version number in a way that is easily accessible to both the setup.py and the package itself:
-
https://www.python.org/dev/peps/pep-0396/
-
https://packaging.python.org/guides/single-sourcing-package-version/
Note that the particular project you linked to is doing it wrong, because they are using a global __version__ module which could easily be overwritten by any other package that tried to use the same technique.
The version typical in the ‘init.py’ file, not version. It’s for storing the version of the software, so you can access it at runtime.
Evening r/Python
The reason for this post is because i am having trouble checking my Python version using the command prompt. Am originally a Mac user, so not sure if the PC transition is what has me confused. Anyways, i know i have Python Version 3.7 installed in my system. However, when i run the python --version command line in both the regular command prompt and administrator command prompt, i get THIS error message.
How come.? This is not really about what version I'm running, as it is about me trying to use the pip install command to install packages. But i figured start at the begging so if command prompt won't even recognize my python installation, why first waste my time trying to figure out why is wont recognize the pip call.
Anyhow, any comments/suggestions would be greatly appreciated. Thank you in advance and good day.! Cheers.! :)
TLDR: I've thrown together a one "page" reference documenting the major changes to between Python versions.
I've spent a fair amount of time recently upgrading some old code-bases and would have found it helpful to have a one page summary of changes between versions. I couldn't find one via Google so decided to create one for myself.
It might be useful for others so sharing it ☺️