Import, From and As Keywords in Python

Updated 19 April 2022

Greetings on your next step in your Python trip. This guide will focus on the | import || from | and | as | keywords. These keywords distinctly focus on ways to introduce new functionality to Python. For stock Python to retain its lightweight and speedy status it allows access only for a small core of modules. Modules are simply Python files which when accessed can allow for unique functionality. So, when extra functionality in a Python script is desired, it is up to the Pythonista to import the desired functions to the script or manually create the functions by hand. This is where the | import | keyword becomes invaluable. Utilising this keyword bestows all the modules you have ever desired right to your fingertips.

Python truly becomes a powerhouse programming language from both the module packages installed automatically and particularly from the external packages you can install yourself. This is why the import statement is so powerful. It is the tool to access these packages. Thus, this tutorial will teach you how to harness the power of modules for any given Python script. This tutorial will provide a thorough overview of import statement and how it works within Python. The contents of this guide can be seen below.

A great place to start your journey would be Python Rundown and Main Windows. These will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand.

As always if you have any questions, queries, or things to add please let us know your thoughts!


Files that the Import Keyword can Utilise

Python Package Index LogoIn real-life coding escapades the import statement is mainly focused on importing built-in modules or downloaded packages. Thus, this guide will focus on the | import | keyword in this regard. The best place to download external packages online is the Python Package Index.

To aid understanding seen below is a quick list of what files can be imported using the | import | keyword in Python. Also, it will clarify what these files are.

- Module. This is referring simply to a file with a .py extension.
- Package. This is a directory containing an __init__.py file and normally a bunch of other modules. The __init__.py files are required to make Python treat directories containing the file as packages.
- Built-in Module. This is a module that is natively installed within Python as soon as you download it.
- Object. This is anything inside a module or package that can be referenced to. These could be a class, a function or a variable.


Import Keyword

A Ball python from the Sahara. If you found this in Oz it'd be an importTo make use of the functions within a module in your script you will need to import the module with an import statement. All import statements are made up of the | import | keyword along with the name of the module. In most Python scripts import statements are declared at the top of the code under any general comments. This is done to ensure they are executed from the get-go.

Importing modules is a critical skill when learning Python. Of the other keywords in this tutorial | import | is the most important. The other two keywords are utilised for running speed and ease of use. Also ensure the module has been properly downloaded when importing a module that is not native to the Python core installation. Installing packages for Python to utilise will be thoroughly explained in a future tutorial.

The code below is written inside the Python Programming Window. It starts by importing several specific modules using import statements. These are random, math and time. Then the new functionality gained from these modules is demonstrated and printed to the Python IDLE Shell. For example, to produce a random integer the function | randint() | has been used. The syntax when referring to a function in this manner requires the module name to be written separated by a period before employing a function of the module.  

Python script that demonstrates the import function


From Keyword

While the import command is very straightforward it lacks efficiency. In many scenarios you may need only a small portion of the module. To solve this the | from | keyword is born. Below is code written in the Python Programming Window. It is a very similar code example to above. This demonstrates the syntax when using the | from | keyword. As you can see this keyword is used in conjunction with an import statement. Also, worth noting you can refer to the new functions directly by name rather than through the period notation seen before. The code below starts by importing several functions from specific modules. Then that functionality is demonstrated and printed to the Python IDLE Shell.

From and Import Keyword Python Script Example


As Keyword

It is possible to modify the names of modules and the name of the functions within Python. This is achieved using the | as | keyword. The desire may come up for a couple of reasons. Perhaps you have already used the same name for something else in your program or another imported module has the same name. Or simply it could be as way to save yourself from writing the function out longhand to save yourself time particularly when your find yourself re-typing the functions many times. 

Code examples of using the | as | keyword can be seen below written in the Python Programming Window. The code starts by importing several functions from different modules and then gives each a shorthand name. Then the functions inside these modules are utilised referencing that shorthand name. The result is then printed to the Python IDLE Shell.

As Keyword in an Import Python Script


The Next Step

You know now how to access files to use in your codes and if you have been following along you also know how to make your own functions and how scope is affected when you do so. The natural next step now in your python crusade would be figuring out how to install packages from external websites to add awesome new functionality to your codes. The best place to learn this would be the tutorial Installing Python Packages


Overview of All Modules

Just for those extra curious (like a PS at the end of a letter) below is an image that lists out every single module that comes installed by default on Python. Using the keyword | import | followed by a space and then any name below will unlock all the functionality of that module to your specific Python script. There are so many! Definitely no need to remember them all but realise this is a big reason why Python dominates as a programming language. Worth noting as well, the external packages for Python dwarf the list below. A great repository for these external packages is the Python Package Index which will be explored further in the next tutorial.

Overview of Every Module using the help function


Download the Codes

Below you can find all the code for the above examples. You will be able to run the codes here on your own computing device and modify the code at your leisure. Open it up with Python IDLE Shell and run it using the Python Programming Window.  

Attachment - python-import-from-as-keywords-script-example.zip

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.