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.
- Open the terminal emulator
- Quick how to tutorial how to open terminal
- 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)
- Result looks similar to this if pip is installed:
- Download file: https://bootstrap.pypa.io/get-pip.py
- 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
- Mac:
- To change the directory in terminal use command
- Run the command
python get-pip.py
(you might need to usesudo
on your system)sudo python get-pip.py
- 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
pip -V
- display version, location, python- if result of this command says you are using
pip
withpython 2.7
and you are usingpython3
- use
pip3
in subsequent examples or create an alias:alias pip=pip3
- if result of this command says you are using
pip help
- display the help how to use thepip
pip help <command>
orpip <command> -h
- display help for command
pip install SomePackage
- installs demo package from PyPIpip show SomePackage
- show information about one or more installed packagespip install SomePackage -U
- upgrade packagepip install SomePackage==0.1.0
- install particular version<=
,>=
- Note: this commands fails due to missing version
0.1.0
ofSomePackage
- There are many more options for installation, check pip install documentation page.
- Note: this commands fails due to missing version
pip list
- List installed packagespip list --outdated
- List installed packages which are out of date.pip freeze
- output installed packages in requirements formatNow 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:pip freeze > requirements.txt
Uninstall our demo package with
pip uninstall SomePackage
pip install -r requirements.txt
- install it again from a requirements filepip uninstall SomePackage
to remove the package.
Now you have learned how to you pip
!
Resources¶
- pip documentation: https://pip.pypa.io/en/stable/
- official installation manual: https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py
- PyPI https://pypi.python.org/pypi - will soon be replaced by https://pypi.org