How can I import functions from another python file

Discussion in 'Python' started by yinglcs@gmail.com, Apr 9, 2007.

  1. Guest

    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/

    Thank you.
     
    , Apr 9, 2007
    #1
    1. Advertising

  2. Lee Harr Guest

    On 2007-04-09, <> 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'
     
    Lee Harr, Apr 9, 2007
    #2
    1. Advertising

  3. En Mon, 09 Apr 2007 18:06:11 -0300, <>
    escribió:

    > i have 2 python files in *different directory* , how can I import
    > python functions from 1 python file to another?


    See the tutorial about modules, packages, and the module search path:
    http://docs.python.org/tut/node8.html
    You can extend the search path using .pth files:
    http://docs.python.org/lib/module-site.html
    (but consider using packages instead of cluttering your search path with
    infinite entries)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Apr 9, 2007
    #3
  4. Shane Geiger Guest


    >> 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.

    That's not exactly correct. You *can* import from files that aren't in
    your sys.path. What follows is a full-working (with python 2.5)
    example. Perhaps ihooks is going to be obsolete at some point, but it
    works now. See PEP 302 for more info. (I'm not sure how to modify this
    example to work with a newer import mechanism or else I would provide it
    to you.)



    import os
    def writefile(f, data, perms=750): open(f, 'w').write(data) and
    os.chmod(f, perms)

    foobar = """
    print "this is from the foobar module"

    def x():
    print "This is the x function."

    """

    writefile('/tmp/foobar.py', foobar)


    # File:ihooks-example-1.py
    import ihooks, imp, os, sys
    def import_from(filename):
    "Import module from a named file"
    if not os.path.exists(filename):
    sys.stderr.write( "WARNING: Cannot import file." )
    loader = ihooks.BasicModuleLoader()
    path, file = os.path.split(filename)
    name, ext = os.path.splitext(file)
    m = loader.find_module_in_dir(name, path)
    if not m:
    raise ImportError, name
    m = loader.load_module(name, m)
    return m

    foo = import_from("/tmp/foobar.py")

    print foo.x
    print foo.x()
    print foo.x()




    > 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'
    >


    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
     
    Shane Geiger, Apr 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    986
    Paul Opal
    Oct 11, 2004
  2. Kurt R. Todoroff

    Dynamically Import HTML From Another File

    Kurt R. Todoroff, Aug 21, 2004, in forum: HTML
    Replies:
    1
    Views:
    823
    Robert Frost-Bridges
    Aug 21, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,652
    Victor Bazarov
    Jun 21, 2004
  4. Jia Lu
    Replies:
    3
    Views:
    319
    Dustan
    Feb 1, 2007
  5. Stéphane Klein
    Replies:
    0
    Views:
    298
    Stéphane Klein
    Jul 5, 2011
Loading...

Share This Page