Learn how to import a Python function from a different file than the one where your main application is coded.
This is a standard practice that allows defining common functions in a single file or module that can then be used by multiple Python programs using the import statement.
In this CodeFatherTech Python tutorial, we will also see the difference between "import {module}" and "from {module} import {function}".
And finally, we will use the Python sys module to add the absolute path where our functions file is located to the Python path.
--------- CHAPTERS ---------
(00:00:00) Create a sample Python program to explain the concepts related to Python import.
(00:01:16) Refactor the initial code using a function.
(00:02:30) Create a new Python file that will contain all the functions used by our program.
(00:03:00) Show how to use the Python import statement.
(00:04:25) Show how to use from ... import to import a specific Python function from another file.
(00:05:40) Import functions from a different file that is not in the same directory as your Python program.
(00:06:30) Use sys.path.append to add a directory to the Python path.
Also, after watching this tutorial let's build a cool game together in Python to keep improving your Python knowledge:
• Code Your Own HANGMAN Game in Python Quick...
Enjoy this tutorial and learn how to import a Python function from another file!
Claudio
#PythonProgramming #PythonTutorial #PythonForBeginners #CodeFatherTech
On this page of the site you can watch the video online How to Import a Python Function from Another File with a duration of hours minute second in good quality, which was uploaded by the user CodeFatherTech 15 December 2020, share the link with friends and acquaintances, this video has already been watched 97,897 times on youtube and it was liked by 871 viewers. Enjoy your viewing!