How to import functions from other projects in Python?

Ideally both projects will be an installable python package, replete with __init__.py and setup.py. They could then be installed with python setup.py install or similar.

If that is not possible, don’t use execfile()! Manipulate the PYTHONPATH to add Foo so that import Project1.file1 works.

For example, from Project2/fileX.py:

from os import path
import sys
sys.path.append(path.abspath('../Foo'))

from Project1.file1 import something

However, the real answer is to make each a discrete installable package.

Leave a Comment