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?
2 Answers
There are two ways you can do that I'm aware of.
Wrong way
import sys
sys.path.append('./supporting_files')
from a import func
func()
Right way
$ touch supporting_files/__init__.py
Then
import supporting_files.a as a
a.func()
2 Comments
Wayne Werner
I mean it works, but works in kind of the same way that a Rube Goldberg machine works. It's fine for entertainment/education but you wouldn't ever want to drive your space program that way.
xxx222
Thanks I think I got it
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.
__init__.pyinsupporting_filesdirectory? What's your python version?__init__.pyin your directory in order to be interpreted as a module, by python.