Using packages in your Python code
We will first have a look at the ways in which third party code and libraries can be downloaded, installed, and included in the Python scripts and application we write.
Importing modules
As we have already seen in several of the examples used so far, Python libraries (modules) are used in code by importing them using the import
statement. However, so far, we have only been importing the entire modules. For example, import threading
imports the entire threading
module and to use, say, the Thread
class, you would have to specify it as threading.Thread
.
Python also allows you to import single classes, and even functions, using a slightly modified syntax, as shown next:
from threading import Thread from time import time
This preceding code imports just the Thread
class and the time
function from the threading
and time
modules respectively. Now when you want to use either within the rest of the file, you would only need to specify either Thread
or time
, rather than...