Which version of Python do I have installed?
python -V
http://docs.python.org/using/cmdline.html#generic-options
--version
may also work (introduced in version 2.5)
How do I check which version of Python is running my script?
This information is available in the sys.version
string in the sys
module:
>>> import sys
Human readable:
>>> print(sys.version) # parentheses necessary in python 3.
2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]
For further processing, use sys.version_info
or sys.hexversion
:
>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192
To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
assert sys.version_info >= (2, 5)
This compares major and minor version information. Add micro (=0
, 1
, etc) and even releaselevel (='alpha'
,'final'
, etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others.
How can I check all the installed Python versions on Windows?
I just got the answer. By typing "py -h" or "py --help" I got the help message:
C:\Users\admin>py -h
Python Launcher for Windows Version 3.7.1150.1013
usage:
py [launcher-args] [python-args] script [script-args]
Launcher arguments:
-2 : Launch the latest Python 2.x version
-3 : Launch the latest Python 3.x version
-X.Y : Launch the specified Python version
The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32 : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64 : Launch the latest 64bit Python X version
-0 --list : List the available pythons
-0p --list-paths : List with paths
Which tells me that "-0" (zero, not letter "O") lists the available pythons:
C:\Users\admin>py -0
Installed Pythons found by py Launcher for Windows
-3.7-64 *
-3.7-32
-2.7-64
-2.7-32
While "-0p" lists not only the versions, but also the paths:
C:\Users\admin>py -0p
Installed Pythons found by py Launcher for Windows
-3.7-64 C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe *
-3.7-32 C:\Users\admin\AppData\Local\Programs\Python\Python37-32\python.exe
-2.7-64 C:\Python27_64\python.exe
-2.7-32 C:\Python27_32\python.exe
How do I test if Python is installed on Windows (10), and run an exe to install it if its not installed?
All of the comments guided me to the right way to do it.
I used this great working code:
:: Check for Python Installation
python --version 3>NUL
if errorlevel 1 goto errorNoPython
:: Reaching here means Python is installed.
:: Execute stuff...
:: Once done, exit the batch file -- skips executing the errorNoPython section
goto:eof
:errorNoPython
echo.
echo Error^: Python not installed
"C:\Program Files\used\systems\innoventiq\accumanager\required\excutables\python-3.7.3-amd64.exe"
How to check all versions of python installed on osx and centos
Use,
yum list installedcommand to find the packages you installed.
How to check python anaconda version installed on Windows 10 PC?
On the anaconda prompt, do a
conda -V
orconda --version
to get the conda version.python -V
orpython --version
to get the python version.conda list anaconda$
to get the Anaconda version.conda list
to get the Name, Version, Build & Channel details of all the packages installed (in the current environment).conda info
to get all the current environment details.conda info --envs
To see a list of all your environments
Detailed description here, download cheat sheet from here
How to ensure I use the correct python version when I have multiple versions of Python installed?
Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself.
They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?):
~$ python3.7 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
~$ python3.8 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
~$ python3.9 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']
The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet.
With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it.
tmp$ python3.7 -m venv app1
tmp$ source ./app1/bin/activate
(app1) tmp$ python -V
Python 3.7.12
(app1) tmp$
(app1) tmp$ deactivate
tmp$ python3.8 -m venv app2
tmp$ source ./app2/bin/activate
(app2) tmp$ python -V
Python 3.8.12
(app2) tmp$
(app2) tmp$ deactivate
tmp$ python3.9 -m venv app3
tmp$ source ./app3/bin/activate
(app3) tmp$ python -V
Python 3.9.9
(app3) tmp$
(app3) tmp$ deactivate
In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python
refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?.
As for installing packages, again, once you have setup the correct virtual environments, doing pip install
would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package.
If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with
$ </path/or/alias/to/specific/python/installation> -m pip install <packages>
$ python3.7 -m pip install "flake8<=3.6"
$ python3.7 -m pip list | grep flake8
flake8 3.6.0
$ ls -H /usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8
flake8
flake8-3.6.0.dist-info
flake8_quotes
flake8_quotes-3.2.0.dist-info
$ python3.9 -m pip install flake8
$ python3.9 -m pip list | grep flake8
flake8 4.0.1
$ ls -H /usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8
flake8
flake8-4.0.1.dist-info
The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder.
But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0.
This means the system default for python
or python3
on your machine points to the 3.10 installation. Mine is set to point to Python 3.9:
tmp$ python3 -V
Python 3.9.9
tmp$ which python3
/usr/local/bin/python3
tmp$ /usr/local/bin/python3 -V
Python 3.9.9
You can:
- Manually change the default version
- Setup aliases to the different versions:
tmp$ type python3.7
python3.7 is aliased to `/usr/local/opt/python@3.7/bin/python3'
tmp$ type python3.8
python3.8 is aliased to `/usr/local/opt/python@3.8/bin/python3'
tmp$ type python3.9
python3.9 is aliased to `/usr/local/opt/python@3.9/bin/python3' - Use a version management tool for switching versions, such as pyenv
How can I find where Python is installed on Windows?
In your Python interpreter, type the following commands:
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
What version of Python is on my Mac?
You could have multiple Python versions on your macOS.
You may check that by command
, type
or which
command, like:
which -a python python2 python2.7 python3 python3.6
Or type python
in Terminal and hit Tab few times for auto completion, which is equivalent to:
compgen -c python
By default python
/pip
commands points to the first binary found in PATH
environment variable depending what's actually installed. So before installing Python packages with Homebrew, the default Python is installed in /usr/bin
which is shipped with your macOS (e.g. Python 2.7.10 on High Sierra). Any versions found in /usr/local
(such as /usr/local/bin
) are provided by external packages.
It is generally advised, that when working with multiple versions, for Python 2 you may use python2
/pip2
command, respectively for Python 3 you can use python3
/pip3
, but it depends on your configuration which commands are available.
It is also worth to mention, that since release of Homebrew 1.5.0+ (on 19 January 2018), the python
formula has been upgraded to Python 3.x and a python@2
formula will be added for installing Python 2.7. Before, python
formula was pointing to Python 2.
For instance, if you've installed different version via Homebrew, try the following command:
brew list python python3
or:
brew list | grep ^python
it'll show you all Python files installed with the package.
Alternatively you may use apropos
or locate python
command to locate more Python related files.
To check any environment variables related to Python, run:
env | grep ^PYTHON
To address your issues:
Error: No such keg: /usr/local/Cellar/python
Means you don't have Python installed via Homebrew. However double check by specifying only one package at a time (like
brew list python python2 python3
).The locate database (
/var/db/locate.database
) does not exist.Follow the advice and run:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
After the database is rebuild, you can use
locate
command.
Related Topics
Why Isn't the Regular Expression's "Non-Capturing" Group Working
Get Number of Items from List (Or Other Iterable) with Certain Condition
Can You List the Keyword Arguments a Function Receives
Adding Borders to an Image Using Python
How Does Python's Comma Operator Work During Assignment
Thread Local Storage in Python
Transform "List of Tuples" into a Flat List or a Matrix
How to Convert an Iterable to a Stream
Running Python Scripts with Xampp
Finding Occurrences of a Word in a String in Python 3
How to Format Axis Number Format to Thousands with a Comma in Matplotlib
Tkinter: Mouse Drag a Window Without Borders, Eg. Overridedirect(1)
Interact with Other Programs Using Python