Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modular Programming with Python

You're reading from   Modular Programming with Python Introducing modular techniques for building sophisticated programs using Python

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785884481
Length 246 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Erik Westra Erik Westra
Author Profile Icon Erik Westra
Erik Westra
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introducing Modular Programming 2. Writing Your First Modular Program FREE CHAPTER 3. Using Modules and Packages 4. Using Modules for Real-World Programming 5. Working with Module Patterns 6. Creating Reusable Modules 7. Advanced Module Techniques 8. Testing and Deploying Modules 9. Modular Programming as a Foundation for Good Programming Technique Index

How to import anything


So far, we have used two different versions of the import statement:

  • Importing a module and then using the module name to access something defined within that module. For example:

    import math
    print(math.pi)
  • Importing something from a module and then using that thing directly. For example:

    from math import pi
    print(pi)

The import statement is very powerful, however, and we can do all sorts of interesting things with it. In this section, we will look at the different ways in which you can use the import statement to import modules and packages, and their contents, into your program.

What does the import statement actually do?

Whenever you create a global variable or function, the Python interpreter adds the name of that variable or function to what is called the global namespace. The global namespace holds all the names that you have defined at the global level. To see how this works, enter the following command into the Python interpreter:

>>> print(globals())

The...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime