views:

35

answers:

3

I run Windows 7, and I can import built-in modules, but when I save my own script and try to import it in IDLE, I get an error saying that the module doesn't exist.

I use the Python text editor found by clicking "File" and "New Window" from the Python Shell. I save it as a .py file within a Module folder I created within the Python directory. However, whenever i type import module_name in IDLE, it says that the module doesn't exist.

What am I doing wrong, or not doing? I've tried import module_name, import module_name.py, python module_name, python module_name.py

+3  A: 

Python uses PYTHONPATH environment variable to define a list of folders which should be looked at when importing modules. Most likely your folder is not PYTHONPATH

Kozyarchuk
So then how do I make the folder PYTHONPATH, and then how do I import the module from that folder? Also, how do I find out the exact directory of that folder?Sorry for the noob questions.
Justin Meltzer
see my answer below
dmitko
A: 

The way to import your own modules is to place the file in the directory of your program and use the following code:

from Module import *

where Module is the name of your module.

Toucan
A: 

Try to add the module's path to sys.path variable:

import sys

sys.path.append(pathToModule)

dmitko
ah, thanks... but how do I find the module's directory path?
Justin Meltzer
you need to find out it by yourself :) usually it's easy to save the module to the predefined location and add this location to the sys.path variable.e.g. if module is in c:\my_modulessys.path.append(r"c:\my_modules")
dmitko
What does the r do in front of the path? Is that raw text?
Justin Meltzer
Ah, just figured it out! I thought I was saving my module as a .py file when I wasn't! Stupid me :/
Justin Meltzer