4

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

5 Answers 5

4

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

1
  • 1
    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. Commented Aug 15, 2010 at 18:16
2

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

import sys

sys.path.append(pathToModule)

3
  • 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_modules sys.path.append(r"c:\my_modules")
    – dmitko
    Commented Aug 15, 2010 at 18:33
  • What does the r do in front of the path? Is that raw text? Commented Aug 15, 2010 at 18:36
  • Ah, just figured it out! I thought I was saving my module as a .py file when I wasn't! Stupid me :/ Commented Aug 15, 2010 at 18:49
0

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.

0

Where are you storing the module you created? When I create a module, I do the following:

  1. Create a folder in the lib\sitepackages folder in your python folder ex: myModules
  2. Save a blank python file named init.py in this folder. The file does not have to contain any syntax. Later on, you can set module requirements in this file if you wish.
  3. Save your module into this folder ex: myGamesModule.py

In idle (or whichever python connected IDE you use) type:

from myModules import myGamesModule or from myModules import myGamesModule as myGmMod

That should import your module and then you can call the classes etc ex myGmMod.Player()

I am sure that this is correct, as I have just done it and it was ok.

0

add a blank file called init.py to the module folder. this tells python that the folder is a package and allows you to import from it the file name should be

__init__.py

stackoverflow keeps formatting my answers and messing thngs up. Thats 2 underscores on each side ofv the word init

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.