The Python Apprentice
上QQ阅读APP看书,第一时间看更新

Importing modules into the REPL

Our module can also be imported into the REPL. Let's try that and see what happens. Start the REPL and import your module. When importing a module, you use import <module-name>, omitting the .py extension from the module name. In our case, it looks something like this:

$ python
Python 3.5.0 (default, Nov 3 2015, 13:17:02)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import words
It
was
the
best
of
times
. . .

The code in your module is executed immediately when imported! That's maybe not what you expected, and it's certainly not very useful. To give us more control over when our code is executed, and to allow it to be reused, we'll need to put our code in a function.