How to Import a Python Function from Another File

preview_player
Показать описание
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.

Also, after watching this tutorial let's build a cool game together in Python to keep improving your Python knowledge:

Enjoy this tutorial and learn how to import a Python function from another file!
Claudio

#PythonProgramming #PythonTutorial #PythonForBeginners #CodeFatherTech
Рекомендации по теме
Комментарии
Автор

your explanation is really very clear ....just like this make short video on other topics in python...thanks.

FYIRE
Автор

In my old laptop (Windows 10) I could follow this procedures and it worked great.
Now, I'm working on a new PC (always Windows 10) but this time commands like "touch" on CMD can't work.
Really no idea what to do...

elghark
Автор

Can u please do a similar video on how to use classes and methods from an imported file .. Would be beneficial 👍👍
Thanks in advance

abhikkumarde
Автор

for some reason i cannot import constants from another file, i have my folder structure exactly as you have it and its throwing a traceback error. im using vs code as a text editor if that means anything

austinmoody
Автор

but my question is, if the file is in a different folder, how do i get access to the functions inside of it?

list
Автор

Nice play with python file...
can you please share code or video for -- call/import/run python file from robot-framework.. actually i tried--- .py contain (class contain -- 3-4 functions)server connecting via paramiko module and doing some stuff and getting some output from server.. and that output using in robot framework... but whenever i trying to import paramiko or any other module getting no keyword found for that function..

basavrajr
Автор

What is that ide name.that doesn't seems like vscode

Andrewtate-sz
Автор

What i dont understand is, you are looping over a original_countries as it is a list but you dont have variable set as a list

pythonpython
Автор

Hi. I'm getting the error "No module named..."

GabrielGcbs