Importing a module
import keyword, you can bring a module into your code. You can then call any subroutines included within that module, in your code.
Here’s an example using the
Every time you run this script, it will print out an integer between 0 and 10.
pip you can install a Python module from the repositories.
Simply run the command
pip install module-name to download a module. You can then import it into your program and use it.
Making a module
Make a new python file with the following content
Then save this in the folder that you wish to call the module from - or the Python module library to call anywhere.
If you called this file
hello.py then you can import it using
import hello and call the procedure it includes using
Code to import and call
And here is the output:
Make sure you’ve saved both files in the same folder, otherwise this won’t work. Additionally, you can save the file you want to import in your Python path or environment, with the core libraries. (Or add your modules folder to your PATH variable)