From previous tutorials of this course, you must have gathered some idea about networking and stuff. Now it's time for us to get our hands dirty. But before that, do you know what are python libraries or python modules? Well, technically a module is simply a Python source file, which can expose classes, functions and global variables. A Python module can be used or imported in another python module. If you are new to python, learn python on Studytonight.
You might think Why do we need modules? Using Modules increase code reusability and helps in easy maintenance of programs. For example: Suppose you are writing a program which is very big. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to reuse the function definition which you created for one program into another without writing or coping the function again. To support this, Python has a way to put definitions in a file and use them in a script. Such a file is called a module.
Let's take an example of a module for adding numbers: Assume
add.py to be a module for adding two numbers.
# Addition Module def add(a,b): #add two numbers 'a' and 'b' c = a + b return c
Now suppose we are creating a
calc.py program and we need a function to add 2 numbers. We can simply import our
add.py module(as import add) in
calc.py for this purpose.
Note: Both the files should be in the same directory for this purpose.
Now to call the
add() method, from the module add.py, we do →
ModuleName.MethodName after successfully importing the module into the program.
>>> import add >>> add.add(9,8) >>> add.add(9.7,5)
The limitless power of python does not end with this. You can import python modules according to your convenience and need. We will be dicussing two ways for installing external python modules to your system, they are:
pip is the preferred installer program. Starting with Python 2.7.9, it is included as default with the Python binary installers i.e Python version > 3.0 will have pre-installed pip installer in it.
For versions < 2.7.9 you have to install pip manually. To install pip in these version follow the below steps:
$ sudo apt-get install python-pip
get-pip.py from here. Then, run it from the command prompt:
You possibly need an administrator command prompt to do this.
If the version of pip is displayed than you are good to go. Now you are ready to download python packages using pip. To download any package using pip :
pip install package_name #Note: Use sudo if you are using linux as non-root user
For example: Say for installing a package named bs4(Beautiful Soup) using pip:
~$ sudo pip install bs4
For detailed knowledge and further problems regarding pip installation follow the official documentation→ https://docs.python.org/2/installing/index.html
Now, the Python Packaging Index is a public repository of open source licensed packages made available for use by other Python users. So, there will be times when some of the required packages cannot be directly downloaded using pip. In that case you need to download and install the required package by third party providers.
tar -xvzf package_name.tar.gz).
python setup.py install
Some sources to download third party libraries are: