Cameron
Cameron

Reputation: 98746

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 folder of foo.py?

Thanks.

Upvotes: 0

Views: 209

Answers (2)

ChristopheD
ChristopheD

Reputation: 116147

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

Upvotes: 2

Khelben
Khelben

Reputation: 6461

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.

Upvotes: 2

Related Questions