Reputation: 433
I am using Python 2.7.12
and I want to check whether the pip is installed or not. For this, in command line of Python application I wrote pip list and pressed enter. However, I get an error like:
File"stdin",line 1
pip list
Syntax Error: invalid syntax
So, how can I solve this issue and get the list of modules as an output?
Thanks
Upvotes: 32
Views: 304915
Reputation: 11
Use below command in command line
python -m ensurepip
This won't only check if pip is installed, it will also install it if missing. It may or may not be what you want to do.
Upvotes: 1
Reputation: 1493
Use command line and not python.
TLDR; On Windows, do:
python -m pip --version
OR
py -m pip --version
Details:
On Windows, ~> (open windows terminal)
Start (or Windows Key) > type "cmd" Press Enter
You should see a screen that looks like this
To check to see if pip is installed.
python -m pip --version
if pip is installed, go ahead and use it. for example:
Z:\>python -m pip install selenium
if not installed, install pip, and you may need to
add its path to the environment variables. (basic - windows)
add path to environment variables (basic+advanced)
if python is NOT installed you will get a result similar to the one below
Install python. add its path to environment variables.
UPDATE: for newer versions of python replace "python" with py - see @gimmegimme's comment and link https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
Upvotes: 40
Reputation: 5055
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'])
Upvotes: 5
Reputation: 388
If you are on a linux machine running Python 2 you can run this commands:
1st make sure python 2 is installed:
python2 --version
2nd check to see if pip is installed:
pip --version
If you are running Python 3 you can run this command:
1st make sure python 3 is installed:
python3 --version
2nd check to see if pip3 is installed:
pip3 --version
If you do not have pip installed you can run these commands to install pip (it is recommended you install pip for Python 2 and Python 3):
Install pip for Python 2:
sudo apt install python-pip
Then verify if it is installed correctly:
pip --version
Install pip for Python 3:
sudo apt install python3-pip
Then verify if it is installed correctly:
pip3 --version
For more info see: https://itsfoss.com/install-pip-ubuntu/
UPDATE
I would like to mention a few things. When working with Django I learned that my Linux install requires me to use python 2.7, so switching my default python version for the python
and pip
command alias's to python 3 with alias python=python3
is not recommended. Therefore I use the python3
and pip3
commands when installing software like Django 3.0, which works better with Python 3. And I keep their alias's pointed towards whatever Python 3 version I want like so alias python3=python3.8
.
Keep In Mind
When you are going to use your package in the future you will want to use the pip
or pip3
command depending on which one you used to initially install the package. So for example if I wanted to change my change my Django package version I would use the pip3
command and not pip
like so, pip3 install Django==3.0.11
.
Notice
When running checking the packages version for python: $ python -m django --version
and python3: $ python3 -m django --version
, two different versions of django will show because I installed django v3.0.11 with pip3
and django v1.11.29 with pip
.
Upvotes: 14
Reputation: 5704
In CMD, type:
pip freeze
And it will show you a list of all the modules installed including the version number.
Output:
aiohttp==1.1.4
async-timeout==1.1.0
cx-Freeze==4.3.4
Django==1.9.2
django-allauth==0.24.1
django-cors-headers==1.2.2
django-crispy-forms==1.6.0
django-robots==2.0
djangorestframework==3.3.2
easygui==0.98.0
future==0.16.0
httpie==0.9.6
matplotlib==1.5.3
multidict==2.1.2
numpy==1.11.2
oauthlib==1.0.3
pandas==0.19.1
pefile==2016.3.28
pygame==1.9.2b1
Pygments==2.1.3
PyInstaller==3.2
pyparsing==2.1.10
pypiwin32==219
PyQt5==5.7
pytz==2016.7
requests==2.9.1
requests-oauthlib==0.6
six==1.10.0
sympy==1.0
virtualenv==15.0.3
xlrd==1.0.0
yarl==0.7.0
Upvotes: 0
Reputation: 3863
$ which pip
or
$ pip -V
execute this command into your terminal. It should display the location of executable file eg. /usr/local/bin/pip and the second command will display the version if the pip is installed correctly.
Upvotes: 10
Reputation: 10092
You need to run pip list
in bash not in python.
pip list
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
argparse (1.4.0)
Beaker (1.3.1)
cas (0.15)
cups (1.0)
cupshelpers (1.0)
decorator (3.0.1)
distribute (0.6.10)
---and other modules
Upvotes: 4