Which python command line

What version of Python do I have?

@TejasKale Better to run ls /usr/bin/python* (or ls /usr/bin/*python* if you really want files with python anywhere in the name). That way, ls still formats its output for a terminal (and you get multiple columns and, with the default ls alias in Ubuntu, colorization).

9 Answers 9

You can use python -V (et al.) to show you the version of Python that the python command resolves to. If that’s all you need, you’re done. But to see every version of python in your system takes a bit more.

In Ubuntu we can check the resolution with readlink -f $(which python) . In default cases in 14.04 this will simply point to /usr/bin/python2.7 .

We can chain this in to show the version of that version of Python:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V' /usr/bin/python2.7: Python 2.7.6 

But this is still only telling us what our current python resolution is. If we were in a Virtualenv (a common Python stack management system) python might resolve to a different version:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V' /home/oli/venv/bin/python: Python 2.7.4 

The fact is there could be hundreds of different versions of Python secreted around your system, either on paths that are contextually added, or living under different binary names (like python3 ).

Читайте также:  Ru adlist css fixes stylus

If we assume that a Python binary is always going to be called python and be a binary file, we can just search the entire system for files that match those criteria:

$ sudo find / -type f -executable -iname 'python*' -exec file -i '<>' \; | awk -F: '/x-executable; charset=binary/ ' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V' /home/oli/venv/bin/python: Python 2.7.4 /media/ned/websites/venvold/bin/python: Python 2.7.4 /srv/chroot/precise_i386/usr/bin/python2.7: Python 2.7.3 /srv/chroot/trusty_i386/usr/bin/python2.7: Python 2.7.6 /srv/chroot/trusty_i386/usr/bin/python3.4: Python 3.4.0 /srv/chroot/trusty_i386/usr/bin/python3.4m: Python 3.4.0 /usr/bin/python2.7: Python 2.7.6 /usr/bin/python2.7-dbg: Python 2.7.6 /usr/bin/python3.4: Python 3.4.0 /usr/bin/python3.4dm: Python 3.4.0 /usr/bin/python3.4m: Python 3.4.0 /web/venvold/bin/python: Python 2.7.4 

It’s obviously a pretty hideous command but this is again real output and it seems to have done a fairly thorough job.

Источник

Check Your Python Version

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Python reigns as one of the most popular programming languages, with a wide range of programs and developer tools relying on it. In fact, your system likely already has at least one version of Python installed.

Many tools and Python development libraries require a particular version of Python. Thus, you may want to know where you can find information on your installed Python version. This can help you make decisions about compatibility, upgrades, and more.

This tutorial shows you how to check your Python version, for both Python 2 and Python 3. Here, you can find the command line method as well as a Python script method for retrieving the current Python version.

How to Check the Python Version from the Command Line

The Python command comes with a command line option of —version that allows you to see your installed version.

It works just as straightforwardly as it sounds. Enter the following command from your command line, and you should get an output similar to the one shown below:

Python 2 vs Python 3

Some systems distinguish between Python 2 and Python 3 installations. In these cases, to check your version of Python 3, you need to use the command python3 instead of python .

In fact, some systems use the python3 command even when they do not have Python 2 installed alongside Python 3. In these cases, you only have the python3 command.

The command for checking the installed version of Python 3 remains otherwise the same — just use python3 with the —version option:

How to Check the Python Version from Python

You can also check your installed Python version from within Python itself. Using either a script or the Python shell, you can use one of the code snippets below to print your Python version.

Both options work equally well regardless of your system. The choice of which option to use really comes down to what format you want the output in.

Using sys

The sys module has a variable you can reference to get the current Python version. Below you can see an example of how the sys module’s version variable renders the current Python version. This code first imports the sys module then prints out the contents of the version variable:

3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]

As you can see, the sys.version variable contains more information about your installed Python version than just the number. For that reason, sys is a good module to turn to when you want more verbose version information.

Using platform

The platform module includes a function that fetches the current version of Python. The example code below uses this function to print the current Python version number. It first imports the platform module; then, the python_version function returns the version number to the print function:

The output from the platform.python_version is more minimal compared to the sys module’s version variable. This makes the platform module more useful for cases when you only need the version number. For example, this method helps when you want to design a program to parse the Python version and act accordingly.

Conclusion

With that, you have everything you need for checking your current Python version. The steps above cover you whether you need to see the Python version from the command line or from within a Python script.

You can continue learning about Python with our collection of Python guides. We cover everything from fundamental Python concepts to building Python web applications.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Monday, August 15, 2022.

Источник

Is there a Python equivalent to the ‘which’ command [duplicate]

Put another way, is there a cross-platform way of knowing which file will be executed by subprocess.Popen(file) without first executing it?

By default, subprocess inherents the environment of the parent process. So for any executable in the PATH (or the OS in question’s equivalent) you don’t need to specifiy the location.

3 Answers 3

Python 3.3 added shutil.which() to provide a cross-platform means of discovering executables:

Return the path to an executable which would be run if the given cmd was called. If no cmd would be called, return None.

>>> shutil.which("python") '/usr/local/bin/python' >>> shutil.which("python") 'C:\\Python33\\python.EXE' 

Unfortunately, this has not been backported to 2.7.x.

Python source for version 3.3’s implementation of shutil.which is here (only a few dozen lines): hg.python.org/cpython/file/6860263c05b3/Lib/shutil.py#l1068

if you change line 1110 to if any([cmd.lower().endswith(ext.lower()) for ext in pathext]) (turn the generator to a list) than it will work in python 2.7.

An option for Python 2 and 3:

from distutils.spawn import find_executable find_executable('python') # '/usr/bin/python' find_executable('does_not_exist') # None 

find_executable(executable, path=None) simply tries to find ‘executable’ in the directories listed in ‘path’. Defaults to os.environ[‘PATH’] if ‘path’ is None . Returns the complete path to ‘executable’ or None if not found.

Keep in mind that unlike which , find_executable does not actually check that the result is marked as executable. You may want to call os.access(path, os.X_OK) to check that on your own if you want to be certain that subprocess.Popen will be able to execute the file.

Also of note, shutil.which of Python 3.3+ has been backported and made available for Python 2.6, 2.7, and 3.x via the 3rd-party module whichcraft.

It is available for installation via the aforementioned GitHub page (i.e. pip install git+https://github.com/pydanny/whichcraft.git ) or the Python package index (i.e. pip install whichcraft ). It can be used like such:

from whichcraft import which which('wget') # '/usr/bin/wget' 

Источник

How to choose which version of python runs from terminal?

I have a few different versions of python on my computer. How do I choose which one is run from my terminal when I type python into the prompt?

6 Answers 6

Use which to see where your python command resides. Then use ls -l to find out where it really is. Then link the one you want instead. Note that the other installed versions are usually all available by their respective names.

$ which python /usr/bin/python $ ls -l /usr/bin/python lrwxrwxrwx 1 root root 9 Jun 18 2013 /usr/bin/python -> python2.7 $ ls /usr/bin/python* /usr/bin/python /usr/bin/python2.7 /usr/bin/python2-config /usr/bin/python2 /usr/bin/python2.7-config /usr/bin/python-config $ sudo ln -sf /usr/bin/python2 /usr/bin/python 

Note that this changes which Python version all programs for all users on your computer will probably use! If you only want to change it for yourself. You can alias it by adding a alias python=’/usr/bin/python2′ line (with python2 replaced by the version you want) to ~/.bashrc in linux or ~/.bash_profile in Mac. (You’ll need to restart your terminal session in this case.)

You should have multiple executables for every python version you have. For example, if I type python and hit tab, I see:

$ python python python2.5-config python2.7-config python3.3 python3.3m-config pythonw2.7 pythonw3.3-32 python-config python2.6 python3 python3.3-32 pythonw pythonw3 python2 python2.6-config python3-32 python3.3-config pythonw2.5 pythonw3-32 python2.5 python2.7 python3-config python3.3m pythonw2.6 pythonw3.3 

So, if, for example, I want python 2.5 version — I run python2.5 .

Also, take a look at virtual environments — it’s much easier to manage and switch between multiple python environments with it.

py -3 or py -2 etc to choose between versions. Even 32/64 bit versions can be differentiated:

To choose which version of python is run when you type ‘python’ into a terminal, you may want to try using an alias.

Would make python2.7 execute when you type ‘python’ into your terminal.

Try envirius (universal virtual environments manager), which allows to compile any version of python. Moreover, it allows to create environments with mixed languages.

One option is update-alternatives :

$ sudo update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.11 2 auto mode * 1 /usr/bin/python3.10 1 manual mode 2 /usr/bin/python3.11 2 manual mode Press to keep the current choice[*], or type selection number: 0 update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python3 (python3) in auto mode $ python3 -V Python 3.11.0rc1 $ sudo update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.11 2 auto mode 1 /usr/bin/python3.10 1 manual mode 2 /usr/bin/python3.11 2 manual mode Press to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python3 (python3) in manual mode $ python3 -V Python 3.10.6 

Источник

Оцените статью