Python language basics 2: how to import libraries in Python


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.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

2 Responses to Python language basics 2: how to import libraries in Python

  1. kif11 says:

    Hi, how do you achieve python snippets in you blog? I use this site

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: