Use function from Python script in OS path
I have a third-party Python script (foo.py) in a folder that is in my system path (but not the Python sys.path). foo.py is not part of any Python module.
I am writing another script (bar.py) in which I'd like to call a function located in foo.py. Is this possible? Can it be done without explicitly naming the folde开发者_如何学编程r of foo.py?
Thanks.
You can include the path of foo.py in the PYTHONPATH environment variable. The interpreter will look also the directories contained there, so you can make the import just like it was on the same directory.
If Python does not find the module, I don't think there's another way then to specify where it can be found, with one easy way being:
import sys
sys.path.append('/myfolder/itsinthisfolder/')
import foo
精彩评论