Import module works in terminal but not in IDLE
Asked Answered
T

9

18

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
Teddie answered 27/7, 2015 at 19:31 Comment(2)
Try getting the version of both idle as well as the python run from terminal , you can use - import sys; print(sys.version) for that.Beatrice
Also look at sys.path for both....see any differences?Roux
I
15

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

Impassive answered 27/7, 2015 at 19:53 Comment(5)
On unix or Mac, one should not remove the system install of Python, even though one normally used an alternate install.Away
@TerryJanReedy Why is that?Adactylous
Because Mac and at least some linux distributions come with Python scripts tested to work with the system Python.Away
This doesn't apply to the OP's error, but I'll add you can get import errors if there is a script with the same name as the package you're trying to import in the same path where you launch IDLE from.Noetic
I was able to uninstall the older version of Python in Windows 11 by going to 'Add or remove programs' and uninstalling the older version. CMD, VScode and Shell were still intact after.Beekman
B
9
  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.

Briefless answered 21/6, 2018 at 8:42 Comment(1)
This worked very well for me too! Thanks.Parlando
J
8

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.
Jesusa answered 1/11, 2018 at 10:57 Comment(1)
It works for me only if I am running from IDE, if I wanna run the .py file from cmd it says no module found. If I wanna run on cmd line by line it gets stuck.Headstrong
O
1

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

Otten answered 5/5, 2020 at 3:2 Comment(0)
C
1

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!!

Colner answered 14/6, 2021 at 7:52 Comment(0)
E
0

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
Epa answered 21/3, 2020 at 20:5 Comment(0)
C
0

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.

Canfield answered 6/8, 2020 at 19:4 Comment(0)
V
0

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.

Vivisection answered 13/1, 2021 at 8:8 Comment(0)
P
0

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.

Pillar answered 1/9, 2021 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.