Calling user defined functions from a different script..

Aaron Brady castironpi at gmail.com
Wed Apr 15 14:51:29 CEST 2009


On Apr 15, 6:33 am, "venutaurus... at gmail.com"
<venutaurus... at gmail.com> wrote:
> Hello all,
>           I have a situation where I need to call functions present in
> a different script whose hierarchy is something like below:
>
> C:\Pythonlib\uitl\script1.py {Place where my functions definitions are
> present}
>
> C:\Scripts\script2.py { Place where function calls are made}
>
>            In such a situation how can I import functions from
> script1.py and use it in script2.py.
>
> Please let me know if you need any further details.
>
> Thank you,
> Venu Madhav.

>From what I understand, you just want a certain line in a certain
file.  The line is the path to the directory you want to import from,
pythonlib\util.  The file is {python_direcotry\}Lib\site-packages
\locals.pth.

Here's mine:

C:\Programs\Python25\Lib\site-packages>type locals.pth
/Documents and Settings/usr/My Documents/
/Documents and Settings/usr/Desktop/working/imptest/

If you want to import multiple directories, you need another file
which will be blank.  The blank file goes in the directory you will be
importing.  It is called '__init__.py'.

Mine:

 Directory of C:\Documents and Settings\usr\My Documents\Lib

04/08/2008  03:35 PM                 0 __init__.py

Then I can do:

C:\Programs\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008
Type "help", "copyright", "credits" o
>>> import Lib

I'm not sure about other versions than 2.5.



More information about the Python-list mailing list