How can I import functions from another python file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yinglcs@gmail.com

    #1

    How can I import functions from another python file

    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.

  • Lee Harr

    #2
    Re: How can I import functions from another python file

    On 2007-04-09, yinglcs@gmail.c om <yinglcs@gmail. comwrote:
    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.appe nd('otherdir')
    >>import amod
    >>amod.afunc( )
    'found'

    Comment

    • Gabriel Genellina

      #3
      Re: How can I import functions from another python file

      En Mon, 09 Apr 2007 18:06:11 -0300, yinglcs@gmail.c om <yinglcs@gmail. com>
      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:

      You can extend the search path using .pth files:
      Source code: Lib/site.py This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s-S option. Importing this module normally appends...

      (but consider using packages instead of cluttering your search path with
      infinite entries)

      --
      Gabriel Genellina

      Comment

      • Shane Geiger

        #4
        Re: How can I import functions from another python file

        >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(fil ename):
        "Import module from a named file"
        if not os.path.exists( filename):
        sys.stderr.writ e( "WARNING: Cannot import file." )
        loader = ihooks.BasicMod uleLoader()
        path, file = os.path.split(f ilename)
        name, ext = os.path.splitex t(file)
        m = loader.find_mod ule_in_dir(name , path)
        if not m:
        raise ImportError, name
        m = loader.load_mod ule(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.app end('otherdir')
        >>>import amod
        >>>amod.afunc ()
        >>>>
        'found'
        >
        --
        Shane Geiger
        IT Director
        National Council on Economic Education
        sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

        Leading the Campaign for Economic and Financial Literacy


        Comment

        Working...