xxx222 xxx222 - 4 months ago 7
Python Question

How to call functions in python files that are not in the working directory?

Say now my working folder is

.
and my supporting python files are in
./supporting_files/
, I want to call a function
func
in the
a.py
file under
./supporting_files/
, what should I do? I tried calling
from supporting_files.a import func
and that does not work. How am I suppose to do that without changing the actual working directory?

Answer

Add an __init__.py file (it can be empty) to the supporting_files directory, and python will treat it as a package available for imports. More details are available in the Python documentation.

Comments