defining functions

Discussion in 'Python' started by Andre P.S Duarte, Apr 5, 2007.

  1. How do I define a function, then import it, without having to save it
    in lib; like "C:\python25\lib". ?
     
    Andre P.S Duarte, Apr 5, 2007
    #1
    1. Advertisements

  2. Andre P.S Duarte

    kyosohma Guest

    Defining a function:

    def someFunc():
    # do something here


    Save the file some place. Import the sys module BEFORE you import your
    module. Then enter the following code:

    sys.path.append(r'path\to\yourFile.py')

    Now import your python module. This will temporarily add that path to
    the python path (or is it system path?) for the amount of time it
    takes to execute your script.

    Mike
     
    kyosohma, Apr 5, 2007
    #2
    1. Advertisements

  3. Andre P.S Duarte

    7stud Guest

    ....or permanently set your PYTHONPATH environment variable to the
    folders you want python to look in for the modules you import. That
    way you won't have to use sys.path.append() to temporarily append the
    path in all your programs.
     
    7stud, Apr 5, 2007
    #3
  4. Andre P.S Duarte

    rweth Guest

    The best way I have found (in windows ) for permanently extending your
    search path for modules is to create the file:
    package.pth
    and put your path/s in a line by line format:
    c:\qa\PythonLibs
    c:\qa\PythonUtils

    The file needs to be in a location in your install
    under the site-packages .. in windows for my system thats:
    C:\Python25\Lib\site-packages\package.pth

    After you do that you can put your modules in the paths you define
    in the package.pth file and sys,path will pick up your modules
    automatically.
     
    rweth, Apr 8, 2007
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.