defining functions

  • Thread starter Andre P.S Duarte
  • Start date
A

Andre P.S Duarte

How do I define a function, then import it, without having to save it
in lib; like "C:\python25\lib". ?
 
K

kyosohma

How do I define a function, then import it, without having to save it
in lib; like "C:\python25\lib". ?

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
 
7

7stud

How do I define a function, then import it, without having to save it
in lib; like "C:\python25\lib". ?

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

rweth

Andre said:
How do I define a function, then import it, without having to save it
in lib; like "C:\python25\lib". ?

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top