Web Programming with Python: Modules

When we close the interpreter after we have tried our Python code, all the functions and logic that we had defined are lost. So when we have to work on longer pieces of code we prefer to save our work in a file and when we have to execute the program it is better to provide the file name as input to the interpreter rather than typing the code directly on the interpreter.

When you work on a bigger project in Python it is a wise thing to split your program into different files so that you are able to easily maintain your code. You can define certain important functions, definitions and classes in a module so that it can be used whenever they are required in a program and when you want to access the functions of that particular module you just simply have to import it in your program file.

Many times Python developers code something interesting and then feel that it would be good to share the functions that they have defined with other developers so they create or package their code in a module and make it available to others online. So, any developer who is looking for something similar can simply down load the module and use it in his work. These modules are completely open source. When you are working in a commercial set up or a closed source you are working with python modules that come under restrictive license but there are several modules that don’t have any restrictive licence.

How to get modules?

Try to find answer to this question on Google and you will be confused by the response. There is too much of scattered information which is the reason why we have designed this tutorial for you. In this tutorial we are going to talk about the present day norm for getting modules and how to make them work in your python project.

First thing that you need to know is that all the important and major modules that we would be using for these series can be installed with the help of pip. Earlier pip was not used for the installation of python modules, there were several ways to install python packages and there was lot of confusion as well. However, now pip comes with python by default and most people prefer to use pip. Installing by pip is really simple you just have to give one command. So, if you have a Windows operating system you will have to give the following command on your command prompt:

pip install module_name

In Windows we give command on the command prompt  whereas in Mac we will use bash and terminal on linux.

When I give a pip command, my system says it’s not a recognized command

If you get such a message when you give a pip command then it means that pip is not in your path.

In order to add pip to the path (for windows, it will vary for other operating systems): go to control panel> system and security>system> advanced system settings. This will open a “System Properties” windows, now go to the advanced tab and click on Environmental variables. It will show you the path, click on Edit button to edit the path. Here, you have to look for the path where Python is installed such as C:/Python 34; is by default  the path where Python 3.4 would be installed. SO, in the same way to access pip you need to find out where it is installed in your system. So, if pip is available at C:/Python34/Scripts/pip then give this path at your command prompt.

People can also have lot of problem in figuring out where their Python installation is. You can find out where your standard library is when you install a module. Suppose you import a module, say the io module:

import io

Now give the following command:

Import io.__file__

This will give you the path for where the file actually is. This seems easy on Windows but it can be little difficult to retrieve this information on Linux or Mac OS.

So, this is how you can install Python modules with pip. However, sometimes pip may not work especially if you have a 64-bit Windows operating system. In such a scenario please go to http://www.lfd.uci.edu/~gohlke/pythonlibs/ as shown in the video.

With this we come to the end of this tutorial on Modules-I.

 

 

GET YOUR FREE PYTHON EBOOK!

(Visited 96 times, 1 visits today)