How to Import a Python Function from Another File
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 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: https://youtu.be/gJBeYcqHNCM