[Tutor] import a module
Kent Johnson
kent37 at tds.net
Wed Dec 21 19:50:28 CET 2005
Christopher Spears wrote:
> I'm feeling a little foolish because I cannot do this.
> I created a module called functions (Clever, huh?) at
> this path:
>
> 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"
>
> However, I keep getting a syntax error on the last
> double quote. According to the docs, I might be able
> to get Python to find this file by modifying sys.path.
> I would prefer not do that because this is just an
> exercise out of the Learning Python book.
You can't give a full path to the import statement. The module to be imported has to be
somewhere in sys.path. Two simple possibilities:
- Open a command line window to C:\Documents and Settings\Christopher Spears\My
Documents\programming\PythonScripts\Part4\Ex07\. Run Python from the command line. You
should be able to import functions because the current working directory is part of sys.path.
- Put functions.py in C:\Python24\Lib\site-packages\. This directory is also part of sys.path.
- (THREE possibilities...) Modify sys.path. You can do this at runtime, just
import sys
sys.path.append(r'C:\Documents and Settings\Christopher Spears\My
Documents\programming\PythonScripts\Part4\Ex07\')
import functions
Kent
More information about the Tutor
mailing list