Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In VS Code, how can I break my project into multiple files?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading