Imports working with raw file, but not in IDLE

3.1k views Asked by At

UPDATE 10 Secs later
Fixed properly now, and thanks to JF and Gauden.

UPDATE
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:

#!/usr/bin/python
# -*- 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("wikipedia.py","f:/ake/pa/th/")

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

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

I then tried adding the path to sys.path, 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:C:\Python27\python.exe, IDLE: C:\Python27\pythonw.exe

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\os.py", line 423, in __getitem__
    return self.data[key.upper()]
KeyError: 'PYTHONPATH'

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://Python27
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin
C:\Program Files (x86)\QuickTime\QTSystem\
C:\Program Files (x86)\Windows Live\Shared
2

There are 2 answers

2
daedalus On BEST ANSWER

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.

0
maccaroo On

I had the same problem when trying to import a newly installed library on my Raspberry Pi. I followed all the instructions to install the library (Adafruit RHT Sensor) and it worked fine from the terminal. However, I couldn't get it to work from within IDLE.

It turned out that the problem was that the Raspberry Pi has both Python 2 and 3 installed. The install I'd done (using the 'python' command) only applied to Python 2. I had to perform another install using the 'python3' command to install it for Python 3. After that, I restarted IDLE and all worked fine.

The suggestion above to print the sys executable path helped point out the discrepancy:

import sys
print sys.executable