[Tutor] import a module

Alan Gauld alan.gauld at freenet.co.uk
Wed Dec 21 22:09:50 CET 2005

And to add to Kent's comments....

>> C:\Documents and Settings\Christopher Spears\My
>> Documents\programming\PythonScripts\Part4\Ex07\functions.py
>> I want to launch IDLE, import the file, and use it.  I
>> tried:
>> import "C:\Documents and Settings\Christopher
>> Spears\My
>> Documents\programming\PythonScripts\Part4\Ex07\functions.py"
> You can't give a full path to the import statement. The module to be 
> imported has to be somewhere in sys.path.

And once you have it in the path you do NOT add the .py at the end.

Simply use

import functions

And to add yet another possibility for setting the sys.path, you can add
your folder to the PYTHONPATH environment variable, by editing
AUTOEXEC.BAT in Win9x or by setting the value in the
"MyComputer->Properties->Advanced" dialog tab.


Alan G.

More information about the Tutor mailing list