Power to Build

Home » Education

Category Archives: Education

Gotcha: Python imports


I am writing some Python scripts to load log files into a database and I included a try…except block like below.

####code###
try:
    # perform some operation - read from file and put in a db table
    ## some other code####
except Exception as ex:

print str(e)
#### more code ###

For some reason, whenever an exception happened instead of the error message I wanted to see, I kept seeing a number like 2.718281 in the log. Suddenly, that number looked familiar.  Isn’t that the natural log base (e) ? Why or how am I printing that?

I decided to troubleshoot that portion of the code. I pulled the try…except block by itself and made it work as a test script, like below. I simplified it by adding a divide by 0, so it will reach the except block. When I ran this, it behaved perfectly.

try:
    # perform some operation - I used a simple one - divide by 0 for example.
    x = 1/0
except Exception as ex:
print str(e)

Python caught it and threw the below exception correctly now..  

Traceback (most recent call last):

File “test.py”, line 6, in <module>
print str(e)
NameError: name ‘e’ is not defined

Do you see that typo there? I named my Exception variable ex and I was trying to print e.  This didn’t throw an exception in my original script. So,  that “e” must have had the value printed. How did it get the value?  Now,  the question seems to Python specific.

I just Googled python and 2.718281. I landed on Python math library. It contains the following constant:

math.e

The mathematical constant e = 2.718281…, to available precision.

There you have it. That’s the number, I have been seeing. That kind of explains it, as I was trying to print(str(e)). That makes sense now as to why my simple program threw an exception. I am not importing any library there, so the name “e” is not found.

Then why did my program find the “e” and print math.e? I am not importing math library. After some trial and error, I found the below line being the culprit:

from pylab import *

pylab probably imports the symbols inside math module. But the point here is the different ways of importing libraries in Python.

Gotcha with Python imports

If you are coming from Java, you only have one type of import statement. Using this, you either import all or specific method from a package.

import java.math.*;

or

import java.math.BigInteger;

That’s all or none. Once you have done that, you have imported all the names into the global name space. Now, you can access the symbols inside java.math just as BigInteger, BigDecimal etc without qualifying them with package names. This may create name collisions, if you import from different modules that have the same symbol(s).

In Python there are 2 ways of importing:

import math

or

from math import e

and a variation of that to import all names in the modulmodule

from math import *

The difference between the 2 approaches is that, the first type (import math) actually only imports the “math” namespace. So, to access anything inside math module, you will have to qualify it:

math. <symbol>

The second approach, particularly, “from <package> import *” is very much like Java import. It imports everything in the math namespace into the global namespace, so you can use symbols from that package without qualifying. That’s how I got my “e” defined suddenly. If there is another module that had e defined, then we would have had a name collision.

So adding pylab import thus into my test script,

from pylab import *
try:
    # perform some operation
    x = 1/0
except Exception as ex:
print str(e)

I have the following output:

2.71828182846

If there is another module that had e defined, then we would have had a name collision. Below example shows such collision. In this case, Python took my file variable:

from pylab import *
# my file global
e = 10.0

try:
    # perform some operation
    x = 1/0
except Exception as ex:
print str(e), str(math.e)

and the output is,
10.0 2.71828182846

So,  here I am forced to qualify math.e.

Learning Opportunities online


In Computer Science, we constantly face the challenge of “keeping up” with technology. If you catch up with one, chances your skills are outdated within a few months or years. Part of the problem has been lack of affordable learning opportunities, just to keep up with trends in the field. Of course, there are courses available in Community colleges and local universities, for continuing education. They were challenging in terms of logistics and time. What if you can learn at your own pace and they are affordable and of good quality? Wait no longer!

Did I say affordable? Read Free! Recently I found that a lot of universities are offering free courses online. These are real courses that are offered in classes in those universities, with home works, programming assignments and even exams. The courses are literally free, including study materials online. Some courses even offer some kind of Certificate. They do not offer any course credits though. The universities include reputed institutions like Harvard, MIT, Princeton, Stanford, UC Berkeley. You can read more about this in this NYTimes article.

For more information on the courses offered, check out the links below.

This blogger has listed a few. MIT has a Open Courseware that offers lectures from their courses online.

I am currently taking a course at Coursera and it has about 60,000 students enrolled from all over the world! It’s definitely opening up new avenues in bringing technology to those remote areas of the world.Personally, if not anything else, I am using it to keep in touch with the trends in Computer Science. This is also a great opportunity to get your hands on newer technology, if you are stuck with old technologies at work!There is one course coming up at edx,org, offered by UC Berkely, called SAAS – Software as a Service. In this course, one can pick up some cloud programming and hands on experience in Ruby on Rails.

If you are a self learner, there are a lot options throughout the web. Youtube has been a great resource for several topics. Some great minds have put together videos explaining complex topics like Algorithms to mathematics modelling. There is also wikibooks and even a wiki university that have great resources for a self learner. But if you need structure and discipline of a classroom to learn, above sites will help.

So, if you want to brush up on those data structures before the next interview, pick up one of those links and have fun learning!