Skip to main content

Section 7.2 Syntax for Importing Modules

  1. The most common is import morecode. That imports everything in morecode.py. To invoke a function f1 that is defined in morecode.py, you would write morecode.f1(). Note that you have to explicitly mention morecode again, to specify that you want the f1 function from the morecode namespace. If you just write f1(), python will look for an f1 that was defined in the current file, rather than in morecode.py.
  2. You can also give the imported module an alias (a different name, just for when you use it in your program). For example, after executing import morecode as mc, you would invoke f1 as mc.f1(). You have now given the morecode module the alias mc. Programmers often do this to make code easier to type.
  3. A third possibility for importing occurs when you only want to import SOME of the functionality from a module, and you want to make those objects be part of the current module’s namespace. For example, you could write from morecode import f1. Then you could invoke f1 without referencing morecode again: f1().

Note 7.2.1. Note: Python modules and limitations with activecode.

Throughout the chapters of this book, activecode windows allow you to practice the Python that you are learning. We mentioned in the first chapter that programming is normally done using some type of development environment and that the activecode used here was strictly to help us learn. It is not the way we write production programs.
To that end, it is necessary to mention that many of the modules available in standard Python will not work in the activecode environment. In fact, only math and random have been completely ported at this point. If you wish to explore any additional modules, you will need to also explore using a more robust development environment.
Check your understanding

Checkpoint 7.2.2.

True / False: All standard Python modules will work in activecode.
  • True
  • Only math, and random have been ported to work in activecode at this time.
  • False
  • Only math, and random have been ported to work in activecode at this time.
You have attempted of activities on this page.