469,343 Members | 5,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

about python modules

hi friends i am new to python programming.
i am using Python 2.5 and IDLE as editor.
i have developed some functions in python those will be calling
frequently in my main method .
now i want to know how to import my functions folder to python in
sucha way that the functions in functions folder should work like
python library modules .

i have python in folder C:\python25\..
and functions folder D:\programs\Functions\

pls help me friends how to do that.
Jun 27 '08 #1
4 1056
In article
<2f**********************************@i36g2000prf. googlegroups.com>,
srinivas <sr**************@gmail.comwrote:
hi friends i am new to python programming.
i am using Python 2.5 and IDLE as editor.
i have developed some functions in python those will be calling
frequently in my main method .
now i want to know how to import my functions folder to python in
sucha way that the functions in functions folder should work like
python library modules .

i have python in folder C:\python25\..
and functions folder D:\programs\Functions\

pls help me friends how to do that.
You need to either:

1) Put your modules in some directory that's already on your python path.
To find out what your path is, do:

import sys
print sys.path

It should include a directory which ends in "site-packages". Just drop
your modules into that directory.

2) Add the directory where you modules are to your python path. The
easiest way to do this is to set PYTHONPATH in your environment.
Jun 27 '08 #2
On 21 Mag, 14:31, srinivas <srinivas.puvv...@gmail.comwrote:
hi friends i am new to python programming.
i am using Python 2.5 and IDLE as editor.
i have developed some functions in python those will be calling
frequently in my main method .
now i want to know how to import my functions folder to python in
sucha way that the functions in functions folder should work like
python library modules .

i have *python in folder C:\python25\..
and functions folder D:\programs\Functions\

pls help me friends how to do that.
You have two choices:

1. In this way you can import single modules (files) in tour folder

import sys
sys.path.append(r'D:\programs\Functions\')
import my_module_1
import my_module_2

and then use whatever you have in the modules:

my_module_1.my_function()
print my_module_1.my_variable
2.
If you add an empty python module called __init__.py inside the folder
D:\programs\Functions\,
then python will handle the folder as a package (i.e. a group of
modules) and you can import
them in this way:

sys.path.append(r'D:\programs\')
import Functions # I'm not sure this is needed ...
from Functions import my_module_1, my_module_2

And then use whatever is in your modules as in case 1.

If you put any code in __init__.py, this code will be executed when
the import Functions
statement is executed. This can be handy in some cases, e.g. if you
have subfolders of
Function folder and want to extend sys.path to include all them.

For more details, read the section 6 of Python tutorial.

HTH

Ciao
------
FB
Jun 27 '08 #3
i always just put most of my python files in the c:\python25 directory.
including ones i want to import as modules, since they import from there.
otherwise you can put the file in c:\python25\lib\site-packages

"srinivas" <sr**************@gmail.comwrote in message
news:2f**********************************@i36g2000 prf.googlegroups.com...
hi friends i am new to python programming.
i am using Python 2.5 and IDLE as editor.
i have developed some functions in python those will be calling
frequently in my main method .
now i want to know how to import my functions folder to python in
sucha way that the functions in functions folder should work like
python library modules .

i have python in folder C:\python25\..
and functions folder D:\programs\Functions\

pls help me friends how to do that.

Jun 27 '08 #4
srinivas wrote:
... i want to know how to import my functions folder to python in
sucha way that the functions in functions folder should work like
python library modules .

i have python in folder C:\python25\..
and functions folder D:\programs\Functions\

pls help me friends how to do that.
An unmentioned possibility:
Create a file named "whatever.pth" (where the "whatever" is your
choice). The contents of this file should be a line containing the
path to your directory (in your case a single line containing
"D:\programs\Functions" (w/o the quotes).
Put this file in your site-packages file (for windows in your case,
that is C:\Python25\Lib\site-packages
--Scott David Daniels
Sc***********@Acm.Org
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
10 posts views Thread by Berthold Hoellmann | last post: by
2 posts views Thread by Olaf Meyer | last post: by
28 posts views Thread by David MacQuigg | last post: by
1 post views Thread by Jerald | last post: by
17 posts views Thread by Jan Danielsson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.