By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,149 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,149 IT Pros & Developers. It's quick & easy.

defining functions

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

Apr 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Apr 5, 1:38 pm, "Andre P.S Duarte" <andre.ditador9...@gmail.com>
wrote:
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

Apr 5 '07 #2

P: n/a
On Apr 5, 12:38 pm, "Andre P.S Duarte" <andre.ditador9...@gmail.com>
wrote:
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.

Apr 5 '07 #3

P: n/a
Andre P.S Duarte wrote:
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.

Apr 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.