There are plenty of libraries for python, from a library to add http capabilities to a library for making games, you should always check if you can find a library for your project that could make your life very easy. Here I’m going to post how to install the ones I currently use in my projects, something that is done generally using pip. In any case, the same process can be use to install any other python library, so feel free to keep adding libraries to python if you need them. As said, here I use pip although you always can install manually all these libraries but, except in the case when they come in a nice package you can just run to install, they can be very difficult (and usually frustrating) to install in our system.
Make Python talk with Arduino by serial port using pySerial.
Arduino is programmed and can be used with its own IDE, which allows to send and receive information using the integrated serial monitor. However, one of the things that I will want to do is to be able to collect that information, sent the data over the serial port and then read it and use it with python (and in the other way around, Python sending information to the Arduino). To do that I require to be able to establish a connection between Python and Arduino through the serial port. Luckily, there’s a small library that enables serial port communication in Python called pySerial. There’s two different ways to install this library: The first one is to download from here the executable file (remember, in our case you have to download it for python 2.7 and 32 bits), run the program and follow the installation steps and remember to check that the installer finds your python installation. The second one, the one I find easier, is to just open the command prompt in windows and write:
pip install pyserial
Just press enter and wait while pip installs the library all by itself. As you can see, using pip and the command prompt it is an effortless way to install libraries in python in Windows*.
*And not only in Windows, you can do it as well in Linux and MacOs.
Installing matplotlib to make beautiful plots with Python
Now we are going to install one of the most used libraries in python: Matplotlib allows 2D and 3D plotting and it is used for hardcopy formats and interactive environments. If you are familiar with programs like MATLAB or Mathematica, this is the alternative in python to plot graphs. To install matplotlib, you can go to the downloads page and get the last version for your python, in this case 2.7 and 32 bits system.
It is possible also to install matplotlib using pip, however; at the time I’m writing this, it seems like there are some problems, in fact, it is not recommended by the matplotlib website itself, so I think that is better to stick with the exe file in this case (I’m sure that the matplotilib creators now a ton more than me about the library). Before using matplotlib for the first time, you need to download some libraries required that aren’t already installed. To do that, we use pip again, we first open again the command prompt in windows and we write:
pip install sixwe press enter and we wait for six to install, then we write:
pip install python-dateutilwe press enter again, we wait for dateutil to install, and finally we write:
pip install pyparsing
and press enter, when is finished, we have all the libraries ready to be used.
At this point, we should have all the necessary to use it, so let’s do a quick test to see if everything is working: We are going to plot a quadratic function with python, the code we are going to use is the following:
import matplotlib.pyplot as plt #import matplotlib library
x=[-3,-2,-1,0,1,2,3] #X values
y=[9,4,1,0,1,4,9] #Y values
plt.plot(x,y, 'b-') #instruction to create the plot with x, v and a blue line
plt.grid(True) #show a grid on the plot
plt.axis([-3,3,0,9]) #set axis limits in the form: x0,x1,y0,y1
plt.title('Test: Quadratic Line') #title of the plot
plt.xlabel('X values') #label for the X values
plt.ylabel('Y values') #label for the Y vlaues
plt.show() #command to show the plot
To test the program, open the program VIDLE for Python that you have installed in the previous post. You can now copy the code above or download matplotlib_example.py from my github repository with examples and open it with the same program. Run it pressing ‘F5’ or by going to ‘Run’ and then ‘Run Module’, in a few seconds you should see something like this:
What to do if I see “ImportError: No module named name_of_the_module”
If you follow this steps, you should have all the required libraries required, but if in any case you’ve missed something (and this is valid for other libraries as well), any time you see this error it means that python can’t find a library, so you probably need to install it, the easiest way is to use pip:
pip install name_of_the_module
If you have received an error, try to download the missing modules and try again. Be aware that you may need to install more than one library.
For the ones that want to see real time data, the last step is drawnow
The last module I have installed is used by Python to show a stream of data. For example, I can have a temperature sensor checking how many degrees I have on the room every 5 minutes and this result is plotted in a graph. That allows me to see how the temperature changes during a day. This module is very easy to install as well and only requires a single line of code using pip:
pip install drawnow
And that’s all the necessary for starters. Eventually you will probably will need to add more libraries to your system and now you already know how to do that using pip. When installing new libraries, just remember to check the documentation on how to install the library. Usually you cannot go wrong using pip but a quick check before installing may save you some troubles and time.