[PyLIB 1] HOW TO INSTALL LIBRARIES IN PYTHON

Hi everybody, following to the series of ‘Basic Python tutorials’, today I want to open another series to introduce Python libraries which are silver bullets that help you improve your weapon to tackle your problems.

In the first tutorial, I will introduce a setup tool called ‘pip’ to help you easily install a Python library and an example of it.

Introduction

As I mentioned before, a feature that makes Python become one of the most powerful weapons is the diversity of libraries. There are many Python libraries which were developed to help developers, scientists, etc to solve their problems quickly and most of them are open-source. So, “you don’t have to reinvent the wheel again, unless you plan on learning more about the wheels” [1].

There are many ways to install a Python libraries. You can install the libraries using .exe or .py file from the library web or using text editor, too name but a few. For me, I prefer to use ‘pip’.

So, what is ‘pip’? It is a package management system that you can use to install, upgrade, and manage libraries or packages in Python [2].

Do you need to install ‘pip’?

If you have Python 2>=2.7.9 or 3>3.4, you will not need to install ‘pip’ and setup-tools because it already install in your Windows along with Python [3]. So, in this case, what you need to do is to upgrade your ‘pip’ to the latest version.

Step 1: Type ‘run’ in search tools of Windows to open ‘run box’.

run_search

Fig. 1 Open run box

Step 2: In ‘run box’ type ‘cmd’ and click ‘OK’ to open ‘Command Prompt’

run_box

Fig. 2 Type ‘cmd’ in ‘run box’ to open ‘ Command Prompt’

Step 3: In ‘Command Prompt’ type:

python -m pip install -U pip setuptools

to upgrade ‘pip’.

For another operating systems, you can find more information on installing ‘pip’ on [3].

Examples

OK, let’s do a warm-up exercise by trying installing ‘matplotlib’ package (which is a graphic library that help user to visualize or plot the graph).

In the first step, you have to open ‘Command Prompt’.

Next, type

pip install matplotlib

and the installing process begins.

This process may take a while. After that, if you get ‘Successfully installed …’. You have already installed this library.

Troubleshooting

In some cases, you may encounter a problem showed in Fig. 3 when you try to upgrade your pip or install lib using pip command.

error

Fig. 3 An error occurs when Python was not added into “Path”

The problem caused by Python was not added into ‘Path’ when you installed Python. So, the Command Prompt don’t understand what pip means.

To tackle this, follow these steps sequentially:

Step 1: Right click “This PC” and select “Properties” (see Fig. 4).

step1

Fig. 4 Open “System” dialog

Step 2: A “System” dialog appears. In which, click “Advanced System settings” (see Fig. 5).

step2

Fig. 5 Click “Advanced System settings” to open “System Properties” dialog

Step 3: In the “System Properties” dialog, click “Environment Variables” (see Fig. 6).

step3

Fig. 6 Click “Environment Variables” to configure system variables

Step 4: In the “Environment Variables” dialog, look for “Path” under the “System variables” window and double click on it (see Fig. 7).

step4

Fig. 7 Double click on “Path” which is under “System variables” window

Step 5: In the “Edit Environment Variable” dialog, click “New” and type “C:\Python27” and then click OK (see Fig. 8).

step5

Fig. 8 Steps to add Python into Path

Step 6: Now, you can try to upgrade pip or install lib using pip command again and see the difference.

In the next tutorial, I will introduce numpy which is one of the most useful package that help user to handle mathematical elements such as vector, matrices, etc.

 

Good luck and hope you enjoy it,

Curious Chick

References

[1] https://blog.codinghorror.com/dont-reinvent-the-wheel-unless-you-plan-on-learning-more-about-wheels/

[2] https://en.wikipedia.org/wiki/Pip_(package_manager)

[3] https://packaging.python.org/tutorials/installing-packages/

Advertisements