I am trying to make a transition from Matlab to Python. I completed the Udemy Python Bootcamp; it used Jupyter Notebook, which was a fine learning environment. Now I’m trying to use VS Code – It has nice debugging features, but I can’t figure out how to break a program into separate files.
In Matlab, my top-level program for a project would call functions that were in separate files in the same folder. I just tried to do the same with a simple Python program in VS Code, and got a weird error message. If I put the function and main program in the same file, then it works.
How can I use separate files for my Python functions, and have the top-level program know how to find them?
>Solution :
You need to import the file at the beginning of your python script.
Example: If you have main.py and function.py and want to use the function test() from function.py, you have to write
the code below in your main.py file.
import function
function.test()
or
from function import test
test()