Imports

This code begins with module imports, which is a fancy way of saying— "someone wrote some code that does something, and I want to use that something in my code". It isn't possible to use libraries, which are just collections of code created by someone, without formally importing them at the beginning of your code. Imports must appear before usage of anything within them because like the English language, python interpreters read from top to bottom.

The module imports are the algod.py file from the v2 client folder, in the algosdk library: library/folder/file.py/class

from algosdk.v2client.algod import AlgodClient

The period " . " between "algosdk" and "v2client" indicates where we are going from the algosdk library, which is the v2client folder. From the v2client folder, we then enter the algod python file, and extract the AlgodClient class to use in our code. This is how you import the file, function, or "class" (we will go over classes later) that you want to use within your code. If we did not import AlgodClient specifically, we could still access it in our code from the Algod file. We would just need to type out the algod_client variable like so:

from algosdk.v2client import algod
algod_client = algod.AlgodClient(node_token, node_port)

We could also stop at v2client, and follow a similar workflow

from algosdk import v2client
algod_client = v2client.algod.AlgodClient(node_token, node_port)

We can see now why sometimes its more efficient to import a specific file, method, or function instead of the entire library— it is certainly easier to read, and write, and is generally much more concise. For the random module, the library comes with python naturally when installed, so you will notice that with a fresh installation of python, you will not have access to the algosdk library, but you will have access to the random library. To gain access to the algosdk library, you would simply need to install it using the pip installer.

pip install py-algorand-sdk

If you don't have pip, it must be installed manually, which is gone over in the Installation guide on this webpage.

import random

To decide on whether or not you want to import a specific file, function, or method, you would need to use your intuition! For example, I use the general import random at the top of my file, yet I only use randint from that module. So it may be better to use

from random import randint

For the sake of understanding, lets just say perhaps the person writing the code unsure of whether or not the code will need other functions from the random library. Perhaps I plan to also use randrange from the random library with randint, so for now I'll import the entire random library.


Quiz

Question 1

What is the correct way to import the AlgodClient class from the algosdk library?





Question 2

Which statement about module imports is true?





Question 3

How would you import only the randint function from the random module?





Question 4

Which command would you use to install the algosdk library using pip?





Code Editor