How to import a file from a subdirectory with Python?

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.