What can we use for External Modules in Python?
A module can simply be defined as any file that contains Python code and is stored with a .py extension. For example, a file named example.py that contains Python statements and definitions can be referred to as a Python module named example.
How can you use External Modules in Python?
External modules in Python are important since they allow developers to break down large programs into small manageable files. Furthermore, modules also allow developers to reuse code.
Once a Python file has been imported as a module into another file it gives us access to its classes and functions.
Although Python gives developers access to a large number of inbuilt modules, we can also create custom modules by defining our own functions and statements in a module and then importing them instead of rewriting them all over again.
Download and install External Modules in Python
External modules in Python can also be downloaded and installed using pip which is a package manager. On the other hand, some modules such as the Math module need no installation. We just need to use the word import followed by the module name. As shown below.
import math
This will give us access to all the functions, classes and statements if any that are within the Math module. So for instance, if we use the keyword ‘math’ followed by a dot we can have access to a list of all functions within the Math module.
As shown below. Using the same principle we can create our own modules and import them quite simply into any file and use their functions as we deem fit.
Using the Sayhey module in Python
The first module that we are going to create is called ‘sayhey.py’. The module is going to contain a very simple function that takes in a variable that we are going to call an animal and return a Hello statement.
Not a particularly complicated one but we can actually import this into a different file and get access to this function within the other file.
Conventionally, we normally do the importing at the top of the other file that we are importing into. In this case, the file that we are importing into is named ‘working.py’.
As shown above, it is not particularly complicated and as shown once we have imported the module sayhey, we now have access to the sayhey.say() function within the other file.
Although the function is not precisely declared in that file, once we pass the argument ‘John’ to the function and run the code we have the Hello statement printed alongside the name John.
Now conversely we can also import data from another Python file. In this case, we are going to import a list of animals from a file named ‘animals.py’.
Since we are importing a second module into the working.py file we need not import it in another line we can chain the imports together.
Import several modules at the same file
Now using the two modules that we have just imported we are going to write a for loop that iterates over the list in the module that we have just imported.
Using the say() function from the sayhey module we can print out the hello statement alongside the names of the animals from the list.
Summary
This is how to use external modules in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
