Python PIP

What is PIP?

PIP is a package manager for Python packages, or modules if you like.

Note: If you have Python version 3.4 or later, PIP is included by default.

What is a Package?

A package contains all the files you need for a module.

Modules are Python code libraries you can include in your project.

Check if PIP is Installed

Navigate your command line to the location of Python's script directory, and type the following:


Check PIP version:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip --version

Install PIP

If you do not have PIP installed, you can download and install it from this page:

Download a Package

Downloading a package is very easy.

Open the command line interface and tell PIP to download the package you want.

Navigate your command line to the location of Python's script directory, and type the following:


Download a package named "camelcase":

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install camelcase

Now you have downloaded and installed your first package!

Using a Package

Once the package is installed, it is ready to use.

Import the "camelcase" package into your project.


Import and use "camelcase":

import camelcase

c = camelcase.CamelCase()

txt = "hello world"


Find Packages

Find more packages at

Remove a Package

Use the uninstall command to remove a package:


Uninstall the package named "camelcase":

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip uninstall camelcase

The PIP Package Manager will ask you to confirm that you want to remove the camelcase package:

Uninstalling camelcase-02.1:

  Would remove:

    c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\camecase-0.2-py3.6.egg-info

    c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\camecase\*

Proceed (y/n)?

Press y and the package will be removed.

List Packages

Use the list command to list all the packages installed on your system:


List installed packages:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip list


Package         Version


camelcase       0.2

mysql-connector 2.1.6

pip             18.1

pymongo         3.6.1

setuptools      39.0.1

Python Try Except

 The try block lets you test a block of code for errors.

The except block lets you handle the error.

The finally block lets you execute code, regardless of the result of the try- and except blocks.

Exception Handling

When an error occurs, or exception as we call it, Python will normally stop and generate an error message.

These exceptions can be handled using the try statement:


The try block will generate an exception, because x is not defined:




  print("An exception occurred")

Since the try block raises an error, the except block will be executed.

Without the try block, the program will crash and raise an error:


This statement will raise an error, because x is not defined:


Many Exceptions

You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error:


Print one message if the try block raises a NameError and another for other errors:



except NameError:

  print("Variable x is not defined")


  print("Something else went wrong")


You can use the else keyword to define a block of code to be executed if no errors were raised:


In this example, the try block does not generate any error:




  print("Something went wrong")


  print("Nothing went wrong")


The finally block, if specified, will be executed regardless if the try block raises an error or not.





  print("Something went wrong")


  print("The 'try except' is finished")

This can be useful to close objects and clean up resources:


Try to open and write to a file that is not writable:


  f = open("demofile.txt")


    f.write("Lorum Ipsum")


    print("Something went wrong when writing to the file")




  print("Something went wrong when opening the file")

The program can continue, without leaving the file object open.

Raise an exception

As a Python developer you can choose to throw an exception if a condition occurs.

To throw (or raise) an exception, use the raise keyword.


Raise an error and stop the program if x is lower than 0:

x = -1

if x < 0:

  raise Exception("Sorry, no numbers below zero")

The raise keyword is used to raise an exception.

You can define what kind of error to raise, and the text to print to the user.


Raise a TypeError if x is not an integer:

x = "hello"

if not type(x) is int:

  raise TypeError("Only integers are allowed")

Python User Input


User Input

Python allows for user input.

That means we are able to ask the user for input.

The method is a bit different in Python 3.6 than Python 2.7.

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

The following example asks for the username, and when you entered the username, it gets printed on the screen:

Python 3.6

username = input("Enter username:")

print("Username is: " + username)

Python 2.7

username = raw_input("Enter username:")

print("Username is: " + username)

Python stops executing when it comes to the input() function, and continues when the user has given some input.