python and pip

pip is used to install packages/modules in python. pypi is the default software repository for python packages. python started packaging pip with python 3.4. For older versions, you might have to install it. You might have multiple versions of python and pip in your machine.


Different versions of pip

node1# pip --version
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
node1# pip3.6 --version
pip 8.1.2 from /usr/lib/python3.6/site-packages (python 3.6)


Basic pip commands

pip  –version

python -m pip install <packagename>

python3 -m pip install <packagename>

python -m pip install –upgrade <packagename>  ## Note: here you don’t have to say pip2 or pip3

python3 -m pip install –upgrade <packagename>## Note: here you don’t have to say pip2 or pip3

pip list ## list installed packages

pip show <module>

pip uninstall <packagename>

How to list all available modules available to your python environment?

help(“modules”) command will give you list of all modules you can use.

node1 # python3 
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help ("modules")


How to install flask in CentOS 7.5?

# python3 -m pip install flask
Collecting flask
  Downloading (94kB)
    100% |████████████████████████████████| 102kB 2.1MB/s 
Collecting click>=5.1 (from flask)
  Downloading (81kB)
    100% |████████████████████████████████| 81kB 3.1MB/s 
Collecting itsdangerous>=0.24 (from flask)
Collecting Werkzeug>=0.15 (from flask)
  Downloading (327kB)
    100% |████████████████████████████████| 327kB 1.8MB/s 
Collecting Jinja2>=2.10.1 (from flask)
  Downloading (125kB)
    100% |████████████████████████████████| 133kB 2.0MB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
Installing collected packages: click, itsdangerous, Werkzeug, MarkupSafe, Jinja2, flask
Successfully installed Jinja2 MarkupSafe Werkzeug click flask itsdangerous
You are using pip version 8.1.2, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Above command will install flask in /usr/local/lib64/python3.6/site-packages/

Now let’s try to import flask module

node1 #python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask

it worked as we installed pip using python3.

node1 #python
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named flask

above didn’t work as python2.7.5 is unaware of the flask module path!


How to get details of a module?

pip show <module>

node1 # pip3 show flask
Metadata-Version: 2.1
Name: Flask
Version: 1.1.1
Summary: A simple framework for building complex web applications.
Author: Armin Ronacher
Installer: pip
License: BSD-3-Clause
Location: /usr/local/lib64/python3.6/site-packages
Requires: Werkzeug, click, itsdangerous, Jinja2
  Development Status :: 5 - Production/Stable
  Environment :: Web Environment
  Framework :: Flask
  Intended Audience :: Developers
  License :: OSI Approved :: BSD License
  Operating System :: OS Independent
  Programming Language :: Python
  Programming Language :: Python :: 2
  Programming Language :: Python :: 2.7
  Programming Language :: Python :: 3
  Programming Language :: Python :: 3.5
  Programming Language :: Python :: 3.6
  Programming Language :: Python :: 3.7
  Topic :: Internet :: WWW/HTTP :: Dynamic Content
  Topic :: Internet :: WWW/HTTP :: WSGI :: Application
  Topic :: Software Development :: Libraries :: Application Frameworks
  Topic :: Software Development :: Libraries :: Python Modules
  flask = flask.cli:main

As you can see there is critical information: It has list of supported python versions, Required packages, OS dependencies etc.

+ There are no comments

Add yours