pip list
is a shell command. You should run it in your shell (bash/cmd), rather than invoke it from python interpreter.
pip
does not provide a stable API. The only supported way of calling it is via subprocess
, see docs and the code at the end of this answer.
However, if you want to just check if pip
exists locally, without running it, and you are running Linux, I would suggest that you use bash's which
command:
which pip
It should show you whether the command can be found in bash's PATH
/aliases, and if it does, what does it actually execute.
If running pip
is not an issue, you could just do:
python -m pip --version
If you really need to do it from a python script, you can always put the import statement into a try...except
block:
try:
import pip
except ImportError:
print("Pip not present.")
Or check what's the output of a pip --version
using subprocess
module:
subprocess.check_call([sys.executable, '-m', 'pip', '--version'])