This article is about a topic that is far more important and even more fundamental than any specific Python tutorial: Libraries.
Why using Python libraries?
Get your hot, freshly-brewed coffee and let me show you what holds you back more than anything else in your coding productivity.
Imagine you’re starting your own Uber business. You need to get access to a car or you cannot even start to sell your services. For sure, you don’t build the car yourself, do you? Why should you reinvent the wheel – literally?
Yet, when it comes to coding many developers DO reinvent the wheel on a daily basis. They implement basic algorithms on their own again and again. What a waste of resources!
In the physical world, if you don’t know how to build a business, you can go to the local library and look it up. Similarly, in the digital world, if you don’t want to invest hours implementing a web crawler, you simply import a library for web crawlers.
Everything is a library in coding – even the programming language itself. The simple statement x = 3 + 2 is only a high-level representation of low-level functionality. In this case, addition is really only a shortcut for multiple machine code instructions.
Coding is nothing but to create high-level functionality out of low-level functions.
What exactly is a Python library?
A Python library is a collection of modules. Many libraries consist of dozens of modules. Some libraries consist of only a single module. This brings us to the next question.
What is a module?
Have you ever put code into a file ‘yourFile.py’ and executed this code via ‘python yourFile.py’? Congrats, you have already created and executed your own module!
Let’s make the concept of modules 100% clear. Say, you want to implement your own module. How do you do this? Simply create two files (just in your head): ‘main.py’ which contains your main program and ‘module.py’ which contains your minimal module.
Puzzle 1: What is the result of executing ‘python main.py’ in your shell? (Find the solution at the end of this article.)
# file module.py print("2+2") # file main.py import module
(Spoiler alert: solve the puzzle first before continue reading.)
The import statement in Python is simply a convenient way to “copy and paste” the content of the module into your file (without ACTUALLY doing this). Note that you must skip the suffix “.py” when importing a module. (Don’t ask me why.)
Puzzle 2: What is the result of executing ‘main.py’?
# file italian_greetings.py def hi(): print("Ciao bella!") def bye(): print("Ciao bella!!") # file main.py import italian_greetings italian_greetings.hi()
Clearly, we are not seeing the result of function bye(). The reason is that we have only defined but not yet executed bye().
Great, now you can import modules from a library (=collection of modules) and you can even create your own modules and libraries! Not too shabby.
However, you will quickly become annoyed by always writing the module name like in italian_greetings.hi().
That’s where a new command comes into play: import x as y. Replace the placeholder x with your (shorter) module name y.
Puzzle 3: What is the result of executing ‘main.py’?
# file italian_greetings.py def hi(): print("Ciao bella!") # file german_greetings.py def hi(): print("Guten Tag!") # file main.py import german_greetings as de import italian_greetings as it de.hi() it.hi()
But how do we get rid of these prefixes completely?
It’s time to introduce a third command (after import x, and import x as y): from x import z. The placeholder x is still a module but z is a specific function in this module. This allows us to do fine-grained imports of just the functions we need. This is useful if we have huge modules with hundreds of functions or if we want to laser-focus our imports.
Puzzle 4: What is the result of executing main.py?
# file main.py from german_greetings import hi from italian_greetings import bye hi() bye()
Next, you’ll learn about a very important topic in Python: pip.
When you install Python, the most useful packages are already pre-installed. These packages form the “Python standard library”.
Although the standard library is huge, it contains only a fraction of all Python packages. If you create your own Python package, it is not included in the standard library by default. So how can other people benefit from your hard work creating your own Python package? And, more importantly, how can you leverage other people’s work? The answer is PyPI.
What is PyPI?
PyPI is an acronym for “Python Package Index”. It’s a HUGE collection of Python packages. PyPI gives you access to over 113,000 Python packages. It’s a powerful source of knowledge!
Each Python package contains many function definitions. Thus, millions of functions are waiting to be leveraged by you! If you have any problem, it is likely that a similar problem is already solved by a much better developer who invested days, if not weeks, into developing an efficient solution.
So how can you access this big honeypot? The answer comes with yet another acronym: pip.
What is pip and how to use it?
Pip is an acronym for the phrase “Pip installs packages” (yes, it’s recursive). And this is exactly what pip does — it installs packages for you. Without pip it’s a pain to install packages: you would have to manually download, decompress, and install the package via “python setup.py”.
With pip, however, installing new packages is as easy as running “pip install ” in your command line. And pip is already installed with most modern Python distributions. It’s already at your fingertips.
So before spending hours creating Python code to solve a problem, search PyPI for relevant Python packages first. If you do not find a package, search even harder. Only if you still do not find a package, consider giving back to the awesome Python community by submitting your own package to the PyPI repository – it’s a simple git request (see the Git cheat sheet I’ve sent you in one of the previous “Coffee Break Python” emails).
I think this is enough for today’s (a bit lengthy) coffee break. Thanks for your time and your attention. It’s my priority to give you a positive return on your invested time!
If you like code puzzles, you will love my book ‘Coffee Break Python: 50 Workouts to Kickstart Your Rapid Code Understanding‘ (links to my book page on this blog). It’s the only puzzle-based learning book on the market (crazy, I know). Check it out!
Here are the solutions to the puzzles in this mail.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.