pip Lab

Source of this lab is hosted at: https://gitlab.com/jans-workshops/pip-lab

Pip is python package manager. In this Hands-On lab you will learn how to install and use pip. All python commands expect you to use python3.

How it works?

By defalt pip uses PyPI - the Python Package Index repository to store installable python packages. It is also possible to configure the pip and use it with your private repository, but this is out of scope of this lab.

Packages installed with pip can be stored at various places such as:

  • system-site - is default system-wide storage directory
  • user-site - is typically located in users profile directory
  • virtual environment - location is same as virtual env settings: .virtualenvs/<ENV NAME>/lib/python3.6/site-packages

Install pip

Some python installation ships with pip already installed if this is your case, feel free to skip this task. There is (usually) no harm if you try to install pip anyway.

  1. Open the terminal emulator
  2. Test if pip is installed: pip -V
    • Result looks similar to this if pip is installed: pip 9.0.3 from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (python 3.6)
  3. Download file: https://bootstrap.pypa.io/get-pip.py
  4. In your terminal navigate to the folder where did you download the file
    • To change the directory in terminal use command cd <path to the direcory>
      • Mac: cd ~/Downloads
      • Win: cd c:\users\<your username>\Downloads
  5. Run the command python get-pip.py (you might need to use sudo on your system)
    • sudo python get-pip.py
  6. If everything went good, you get the message:
    • Successfully installed pip-9.0.3 setuptools-39.0.1 wheel-0.30.0

Using pip

Before installing the package make sure you are using correct pip for python3

  1. pip -V - display version, location, python

    • if result of this command says you are using pip with python 2.7 and you are using python3
    • use pip3 in subsequent examples or create an alias: alias pip=pip3
  2. pip help - display the help how to use the pip

    • pip help <command> or pip <command> -h- display help for command
  3. pip install SomePackage - installs demo package from PyPI

  4. pip show SomePackage - show information about one or more installed packages

  5. pip install SomePackage -U - upgrade package

  6. pip install SomePackage==0.1.0 - install particular version <=, >=

    • Note: this commands fails due to missing version 0.1.0 of SomePackage
    • There are many more options for installation, check pip install documentation page.
  7. pip list - List installed packages

  8. pip list --outdated - List installed packages which are out of date.

  9. pip freeze - output installed packages in requirements format

    Now if your project is finished and you want to distribute it, you can write down requirements file in the same format or create it like this:

    1. pip freeze > requirements.txt
  10. Uninstall our demo package with pip uninstall SomePackage

  11. pip install -r requirements.txt - install it again from a requirements file

  12. pip uninstall SomePackage to remove the package.

Now you have learned how to you pip!

Resources