functions - where to store them

P

plsullivan1

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.
The problem: do I have to cut and paste functions into a script or can
I store them in a directory and call them from a script in another
directory. If the latter is possible, how is this done? Thanks.
 
G

Gabriel Genellina

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.
The problem: do I have to cut and paste functions into a script or can
I store them in a directory and call them from a script in another
directory. If the latter is possible, how is this done? Thanks.

I suggest you read the Python tutorial at http://docs.python.org/tutorial
In particular, you're looking for "modules", and they're covered at
http://docs.python.org/tutorial/modules.html

"""If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost. Therefore,
if you want to write a somewhat longer program, you are better off using a
text editor to prepare the input for the interpreter and running it with
that file as input instead. This is known as creating a script. As your
program gets longer, you may want to split it into several files for
easier maintenance. You may also want to use a handy function that you’ve
written in several programs without copying its definition into each
program.

To support this, Python has a way to put definitions in a file and use
them in a script or in an interactive instance of the interpreter. Such a
file is called a module; definitions from a module can be imported into
other modules or into the main module."""
 
M

Marco Mariani

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.

read the tutorial, look for "modules" and "packages"
 
P

plsullivan1

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.
The problem: do I have to cut and paste functions into a script or can
I store them in a directory and call them from a script in another
directory. If the latter is possible, how is this done? Thanks.

Nevermind... it's like buying something to replace what was lost only
to find the lost one.
 
L

Lie Ryan

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.
The problem: do I have to cut and paste functions into a script or can
I store them in a directory and call them from a script in another
directory. If the latter is possible, how is this done? Thanks.

Yes of course. It is called module.

file: functioncoll.py

def foo(): pass
def bar(): pass

file program.py
import functioncoll

functioncoll.foo()


Note: functioncoll.py must be in your python search path. One way to
easily ensure it is in the search path is to put functioncoll.py in the
same directory as program.py

Note that all the usual tricks with import also works, like
from functioncoll import foo, bar
import functioncoll as fc
 
T

Terry Reedy

I have several functions which I would like to store in a different
directory so several programs can use them. I can't seem to find much
information about how to call a function if the function code is not
actually in the script itself.
The problem: do I have to cut and paste functions into a script or can
I store them in a directory and call them from a script in another
directory. If the latter is possible, how is this done? Thanks.

After you follow the other suggestions of read about 'module' and
'package'.... If you want a module or package available to *all*
programs (that run with a particular version/installation of Python),
you can put your module or packages in the 'site-packages' directory.
On Windows, this is a subdirectory of pythonxy/lib/
['C:\\Programs\\Python30\\Lib\\idlelib',
'C:\\WINDOWS\\system32\\python30.zip', 'C:\\Programs\\Python30\\DLLs',
'C:\\Programs\\Python30\\lib', 'C:\\Programs\\Python30\\lib\\plat-win',
'C:\\Programs\\Python30', 'C:\\Programs\\Python30\\lib\\site-packages']
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top