Monday, March 26, 2012

#exclude < /* Guess the*/ Error.lib>


Hey readers, this blog is going to be related to the fun and excitement of programming stuff. The blog is named DeKode as I believe it the most important ability which a programmer needs to excel in the Geeky world of IT. All posts in this blog will be bits and flips of code. I will not make it as boring as it sounds and will try to give vague idea for all technical stuff I include. I am writing this blog to share my happiness which I experience while programming and to give you some knowledge even.

I was working on my open-source project and as a part of it I was suppose to learn basics of matplotlib in few hours. "matplotlib" is a standard library in python used for plotting stuff. I thought it will be easy, just type the command sudo apt-get install python-matplotlib( just think it as setup.exe file in windows) and I will be done with the installation. Basic steps to start using a library or tool is to get the code, build it and then use it.

After installing numpy, ipython and matplotlib I was ready to use it and get my work done.I assume you have a vague idea of "unix package terminology", and so here we use packages and build them before using them. After doing so, I created a basic python file named "new.py" which was expected to plot a straight line shown below.



Code for "new.py"
.
<pre class="brush: python"> # new.py # import numpy as np import matplotlib.pyplot as plt plt.plot([1,2,3,4]); # end of file # command-----python new.py </pre>

The code is quite simple but still it didn't work the way I expected. Errors are always ready to interrupt coders and I was no exception. The error was "Attribution error" not able to find matplotlib.pyplot . I tried but was not able to DeKode the error, so decided to go to my friend Sid who is a "pro" of programming stuff. He checked the version number and dependencies of numpy and ipython available on my machine as an initial check. And numpy was present of version 1.5.1 which is not the latest. I said how is that possible as I downloaded it minutes before and how can a new version be up in a minute.

So here is the "new" thing, when we use apt-get install we download package form ubuntu repository and ubuntu manages to give us the latest version it has. The problem was it had 1.5.1 as latest version, but which was outdated as per python repository. So solution was to download it from python repository directly using pip (python package index)(a tool to download form python repository). So I did apt-get install python-pip and then used pip command

pip install --upgrade numpy

to finally get it done.

I was amazed to learn new stuff like this, and was expecting it to work fine now.
But as the name of the post suggest Guess the error...it can't be that easy. The error remained same ""Attribution error "new.py" line 2 undefined reference to matplotlib.pyplot"".

Just try to imagine what the error could be......(version number was not the reason for error)

We tried to run the command """import matplotlib.pyplot""" on python command line interpreter(an tool to check the working of commands directly) instead of running the file. And it gave the same error as before.....

It still didn't strike us initially what the problem was, but I hope you got the catch.....

If you didn't then it is here for you.....

How can it give the same error, do you remember the error

"Attribution error "new.py" line 2 undefined reference to matplotlib.pyplot"".

If I didn't tell the interpreter that I have a file new.py and it has same command then how can it refer to the file new.py.(if you remember I just wrote import matplotlib.pyplot).

Hopefully now everybody got the error.....it was in naming of file as "new.py".

So never name it new.py .....;)

The problem was numpy imports a file named new from it's other library files.
And while compiler checks for finding header files it looks first in current directory and then in usr/local/include. So it found new.pyc file in my current directory and started to use it, but it was not what it wanted....it was the object file of new.py which I created and not a predefined one with function prototypes.

Sid was the one who decoded the error, renamed the file new_.py and it worked perfectly.

I learned a lot of things in fixing such a silly bug and I hope even you got something out of it.

So it's all about the ability to DeKode.

Cheers
Enjoy Programming