Python language basics 2: how to import libraries in Python
February 28, 2015 2 Comments
Introduction
In the previous post we made our first steps into the world of Python. We installed the SDK and an IDE called PyCharm. In this short post we’ll see how to import external libraries into a Python file.
The import keyword
The keyword to import an external library into your Python project is simply “import”, like in Java.
Here’s a simple import statement to include the “calendar” library:
import calendar
Let’s just reinforce what we saw in the previous post. We didn’t need a semicolon or any other delimiter at the end of the statement. That’s one of the features that distinguishes Python from other popular C-like programming languages like C# or Java.
You can also import a single component within a library. The following statement will import the “formatstring” method from within the calendar library:
from calendar import formatstring
The formatstring method will then become available in your python source file.
You can also give the imported component an alias:
from calendar import format as fc
You can then call the formatstring method as “fc(arguments)”.
In the next post we’ll look at the role of white space in Python.
Read all Python-related posts on this blog here.
Hi, how do you achieve python snippets in you blog? I use this site http://hilite.me/
Hello, I use WP’s built-in code posting markup.
//Andras