Imports working with raw file, but not in IDLE

后端 未结 2 1055
南方客
南方客 2021-01-23 21:15

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

UPDATE
I have found a temporary fix by saving the IDLE fi

相关标签:
2条回答
  • 2021-01-23 22:01

    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
    
    0 讨论(0)
  • 2021-01-23 22:09

    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 讨论(0)
提交回复
热议问题