Kuoso
Kuoso

Reputation: 181

Import module works in terminal but not in IDLE

I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:

Traceback (most recent call last):

  File "FilePath/Filename.py", line 3, in <module>
      import pyodbc
  ImportError: No module named pyodbc

Upvotes: 18

Views: 67696

Answers (9)

AKD
AKD

Reputation: 91

  1. Open python in cmd (type python and press enter)
  2. Import the module in cmd (type import modulename)
  3. Type modulename.__file__
  4. You will get the path where the module is stored
  5. Copy the corresponding folder
  6. In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
  7. Paste your modules folder in the path where IDLE looks for modules.

This method worked for me.

Upvotes: 9

Rishabh Vatsa
Rishabh Vatsa

Reputation: 31

For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path. then open idle and run these lines

import sys
sys.path

Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle

sys.path.append("Enter the copied path of package here")

After executing these lines, check again by importing the package that if it works for you.

Upvotes: 0

Jethan
Jethan

Reputation: 71

Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.

ModuleNotFoundError

How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter

  • Open your Terminal & execute the below commands :

    :~$ python3

    Python 3.6.9 (default, Jan 26 2021, 15:33:00)
    [GCC 8.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>>
    >>> import sys
    >>> sys.version
    '3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
    >>> sys.path
    ['', '/usr/lib/python36.zip', '/usr/lib/python3.6', 
    '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist- 
    packages', '/usr/lib/python3/dist-packages']
    >>>
    

Now import your module inside of your python3 interpreter.

  >>> import nester
  >>>
  >>> nester.__file__
  '/usr/local/lib/python3.6/dist-packages/nester.py'
  >>>
  • Open your IDLE and run the below commands and compare them

    Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 
    64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more 
    information.
    >>> import sys
    >>> sys.version
    '3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit 
    (AMD64)]'
    >>> sys.path 
    

['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']

  >>> sys.executable

'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'

Now if you compare both outputs from Terminal & IDLE,

  • Terminal Module location is different from IDLE
  • I was using Ubuntu 18 terminal on windows machine

So I just copied my file to 'C' directory and ensured its file privileges. That's it.

  :~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/

ModuleNotFoundError Fixed

It worked!!

Upvotes: 1

Ved
Ved

Reputation: 1

Check the path of your code, and that of the module. Copying the module to the path where code is worked for me. 'sys.executable' will give the path where code is stored.

Upvotes: 0

anonymous
anonymous

Reputation: 1

When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.

Upvotes: 0

zerocool
zerocool

Reputation: 3502

this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this

cd to the dir of the version that the import work fine in cmd in that folder type this command below

pythonw.exe Lib\idlelib\idle.pyw

this command will invoke idle for that version and the import will work fine

Upvotes: 1

Nuwan Abeynayake
Nuwan Abeynayake

Reputation: 175

I Found the solution. It works for me

The problem is your installation directory does not match with the python version directory.
solution is >>>

  1. type %localappdata% in your search bar then go to this folder.
  2. here select the program folder. then select Programs , Python , Python version , Scripts
  3. copy the location of the Scripts folder
  4. open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
  5. if you wanna install numpy , now run pip install numpy

Upvotes: 0

dmgl
dmgl

Reputation: 267

You can pip show after install package and know about location where package installed.

After that check in IDLE sys.path and if directory with package not in sys.path try to add it.

import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.

Upvotes: 8

Garrigan Stafford
Garrigan Stafford

Reputation: 1403

This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using

import sys
sys.version
sys.path

These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install

Upvotes: 15

Related Questions