How to Import a Python Function from Another File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello it's claudio and in this tutorial we will see how to import a python function from another file but before doing that let's create a simple program that we can use for our tutorial after defining a list we will write a for loop that goes through all the elements of our list and generates a new list containing only the elements starting with the capital s and now let's refactor our code to include the for loop inside a separate function we will call the function filter underscore countries before making any more changes let's confirm that our code works well when we call the new function then we create a new file inside the current directory the name of the file will be functions.py this will be the file that will contain all the functions used by our program the first step is to copy the function we have defined in our first file into the functions.py file the idea is that we want to be able to execute the function from our main file referencing the function in the functions.py file to import the function from the functions.py file we will use the python import statement we will also modify the call to the filter underscore countries function to include the name of the module or name of the file so functions followed by dot so the call to our function will become functions dot filter underscore countries as you can see our program is working fine now i want to show you a different technique that you can use to import functions from a different file in your python program let's define a new function in the functions.py file the name of the new function will be getfirstcountry that basically just returns the first element of the countries list when we call this second function from our main python program the program works as expected let's say you want to reduce the amount of code you import into your python program to do that instead of importing the full functions file we will import only the filter underscore countries function to do that we will use from functions import filter underscore countries as you can see we are getting an error back name functions is not defined that's because we have not imported the full functions file so let's remove functions dot from the to print statement when you use from module import name of the function in your python program you can call the function without referencing the module name when we run our program again we get another error the error is get first country is not defined can you see the reason that's because we have only imported the filter countries method from the functions file we have not imported the get first country method so let's add another import statement for the second function our program is working again let's see how we can import the functions file if that file is not in the same directory as our main python application to do that i will create a lib directory in the current directory and i will move the functions.py file in the leap directory when we run our program we get back a module not found error and that's because the python interpreter cannot find the functions file or functions module inside the current directory to solve this problem we will change the import statements that will become from lib.functions import function name now i will remove leap dot from our import statement because i want to show you a different way to import a function from a python file that is not located in the current directory first we import the sys module and then we use the method sys.path.append to add the lib directory where the functions.py file is located to the python path where the python path is a list of directories used by the python interpreter to look for modules and files as you can see after adding the absolute path of the leap directory to the python path our program works fine subscribe to godfather for more step-by-step tutorials and to bring your python programming knowledge to the next level you
Info
Channel: CodeFatherTech
Views: 90,886
Rating: undefined out of 5
Keywords: python import modules from different directory, python import tutorial, python tutorial, python for beginners, python programming, import python file from another folder, import python module in visual studio code, python function from another file, python function from different file, how to import file in python, python, python3, python import, python import module, import a python function from another file, python import own module, python import own file, python import file
Id: jx4eysaLTrg
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Wed Dec 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.