简体   繁体   中英

problem in checking whether python 3 is installed

I want to check whether python 3 is installed on my Ubuntu. I'm using this script:

#!/usr/bin/env sh

#install firefox if does not exist
if ! command -v python3 >/dev/null 2>&1
        then
                echo "not installed"
        else
                echo "installed"
fi

When I run this script, the output is installed , but when I check my python using python --version , I get this output:

Python 2.7.17

Which, as far as I know, means that the latest version of python on my Ubuntu is 2.7 not 3.x. What's wrong?

The output of command -v python3; echo $? command -v python3; echo $? :

/usr/bin/python3
0

The output of ls -l /usr/bin/python3 :

lrwxrwxrwx 1 root root 9 Nov 14 09:13 /usr/bin/python3 -> python3.7

The output of ls -l /usr/bin/python3.7 :

-rwxr-xr-x 2 root root 5102632 Oct 27 11:43 /usr/bin/python3.7

The output of which python :

/usr/bin/python

The output of ls -l /usr/bin/python :

lrwxrwxrwx 1 root root 7 Jan 19 08:04 /usr/bin/python -> python2

The output of ls -l /usr/bin/python2 :

lrwxrwxrwx 1 root root 9 Jan 19 08:04 /usr/bin/python2 -> python2.7

Also I have another Ubuntu on a separate VM, the output of python --version , returns command 'python' not found , but when I execute the above commands for this VM as well, it returns similar responses (indicating that python is installed).

https://www.python.org/dev/peps/pep-0394/

Note: You can have an environment where you have both Python 2 and Python 3 installed, and python can point to either. (Most likely python2 for backward compatibility purpose.) Python 3 applications should always use the python3 command and not python .

Try the whereis command. It will tell you where python3 is.

whereis python
Or better yet
whereis python3

The output should be a path, or a list of paths. You might see something like:

python3: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3.6m-config
/usr/bin/python3 /usr/bin/python3.6-config /usr/lib/python3.6 /usr/lib/python3
/usr/lib/python3.7 /usr/lib/python3.8 /etc/python3.6 /etc/python3 /usr/local

Then make sure your PATH variable has at least one of the directories above within it. The PATH environment variable is essentially a list of directories that contain executables (programs). When you type something on your command line your terminal program will search the directories listed in PATH for the executable you specified.

echo $PATH

/home/USER/.pyenv/shims:/home/USER/.pyenv/bin:/home/USER/esp/xtensa-esp32-
elf/bin:/home/USER/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

You might have a directory in your PATH that is messing things up, or you might need to add a new directory.

Because so many programs have been written using Python 2, many operating systems keep Python 2 in their repository, and this isn't going to change any time soon.

So when you installed Python, it added Python 2 to /usr/bin/ (maybe /usr/bin/python2, maybe /usr/bin/python2.7, etc.), and pointed /usr/bin/python to the same location. When you installed Python 3 it also installed Python 3, to /usr/bin/python3.

When you test whether python3 is installed, you find that it is. According to PEP 394 , /usr/bin/python should refer to Python 2. Ubuntu documentation explains what that means and what it doesn't:

What this does not mean:

/usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.

Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020. It would be nice if we could demote Python 2 to universe, but that's currently problematic for technical reasons relating to multi-Python version support in Debian/Ubuntu.

Basically, while all development should be geared toward Python 3, the python command (/usr/bin/python) should point to Python 2 in order to keep current programs from breaking .

If you'd like to access Python 3, it's recommended that you call python3. (You could also rebind /usr/bin/python to point to python3, but this is highly unrecommended. A more useful solution to most users would be to create an alias to python3 .)


Short version: Your script works. Python 3 is installed. If you want the terminal to open Python 3 when you type python , add an alias alias python=python3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM