ACarter ACarter - 2 months ago 12
Python Question

Imports working with raw file, but not in IDLE

UPDATE 10 Secs later

Fixed properly now, and thanks to JF and Gauden.


I have found a temporary fix by saving the IDLE file in the directory the other working file is in. (I would still like to know how to fix it entirely if I can.)

That's not a permanant fix, so if you want to try and help make it work wherever the file is saved, feel free.

This is the start of a python file:

# -*- coding: utf-8 -*-
import wikipedia
import pagegenerators
import sys
import re
import pywikibot
from pywikibot import *

(You may have noticed it's a pywikipedia script, but I think that's irrelevent)

This file runs fine from the command line.

However, when I try and use IDLE to develop the script, or just use the IDLE interpreter, I get an error:

>>> import wikipedia

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
import wikipedia
ImportError: No module named wikipedia

I don't really have a clue why it isn't working.

I have also tried this:

>>> imp.find_module("","f:/ake/pa/th/")

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
ImportError: No frozen submodule named f:/ake/pa/th/

The path given in the error log is the correct path to the file, there's just that

I then tried adding the path to
, but that didn't work either:

>>> import sys
>>> sys.path.append("c/users/adam/py")
#the same error...

Path to the module: `c:\users\joe_bloggs\py\wikipedia.pyc

Python executable: Command line:

PYTHONPATH throws, in both:

Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
print os.environ['PYTHONPATH'].split(os.pathsep)
File "C:\Python27\lib\", line 423, in __getitem__

OS: Windows 7

Python version: 2.7.2

A new PATH: IDLE, and Command Line:

C:\Program Files\Common Files\Microsoft Shared\Windows Live
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin
C:\Program Files (x86)\QuickTime\QTSystem\
C:\Program Files (x86)\Windows Live\Shared


EDIT The answer to the above question proved to be fairly simple, but I am editing this answer as a possible troubleshooting checklist for future reference, and as a checklist for others who may need to prepare questions of this nature in the future.

CLUE 1: What is the path to the module you are importing?

>>> import wikipedia
>>> print wikipedia.__file__

This will give you the path to the compiled module, and is one clue.

CLUE 2: What is the path to the Python executable?

(See also this question).

>>> import sys
>>> print sys.executable

Try this in the shell and in an IDLE script. If the two results are different, then you are using two Python interpreters and only one of them has a path that points to the wikipedia module.

CLUE 3: What is the sys.path?

Also repeat this in both shell and as a script in IDLE.

>>> print '\n'.join( sys.path )

(You may be able to use sys.path.append("d:/irectory/folder/is/in") to add that location to the sys.path. This should add that directory to the list of places Python looks for modules.)

CLUE 4: What is the PYTHONPATH and does it differ in the two environments?

(See also this answer).

Finally repeat this in both shell and as a script in IDLE.

>>> import os
>>> print '\n'.join( os.environ['PATH'].split(os.pathsep) )

Again note the two results (from shell and from IDLE) and see if there is difference in the PYTHONPATH in the two environments.

If all these tests prove inconclusive, I would add as much of this information as you can to your question as it would help give you specific further leads. Also add what OS you are using and any tracebacks that you get.