about python modules

S

srinivas

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

Roy Smith

srinivas said:
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.
 
B

bockman

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
 
I

inhahe

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
 

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
474,285
Messages
2,571,415
Members
48,107
Latest member
jigyasauniversity

Latest Threads

Top