How can I import functions from another python file

Lee Harr missive at frontiernet.net
Mon Apr 9 17:44:40 EDT 2007


On 2007-04-09, yinglcs at gmail.com <yinglcs at gmail.com> wrote:
> Hi,
> i have 2 python files in *different directory* , how can I import
> python functions from 1 python file to another?
>
> i get this error:
> import task
> ImportError: No module named task/
>


The directory that module is in must by on your python
path in order to import it. You can do it by modifying
sys.path or by setting the PYTHONPATH env variable.


$ mkdir otherdir
$ cat > otherdir/amod.py
def afunc():
    return 'found'
$ python
>>> import amod
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named amod
>>> import sys
>>> sys.path.append('otherdir')
>>> import amod
>>> amod.afunc()
'found'



More information about the Python-list mailing list