Categories
Python Answers

How to import a file from a subdirectory with Python?

Spread the love

Sometimes, we want to import a file from a subdirectory with Python.

In this article, we’ll look at how to import a file from a subdirectory with Python.

How to import a file from a subdirectory with Python?

To import a file from a subdirectory with Python, we can add an __init__.py file into the root of the subdirectory.

Then we can import the modules from the subdirectory.

We can leave the file empty.

This will mark the subdirectory as a directory that we can import modules from.

Conclusion

To import a file from a subdirectory with Python, we can add an __init__.py file into the root of the subdirectory.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *