Sunday, July 10, 2011

php:include "bar.php" 2 python:?

PHP programmers are used to having code in one file they can use in another file simply by calling include. Python's module system supports more than just including files, but if you want to just include a file the following code provides an example:

> ls 
foo.py mod/
> ls mod/
bar.py
> 
> cat mod/bar.py 
def f():
    print("I am bar")
> 
> cat foo.py 
print("I am foo")
import sys
sys.path.append("mod/")
import bar
bar.f()
> 
> python foo.py 
I am foo
I am bar
> 

No comments: