6

I'm trying to set up a script with SQL connection, and am running into the following error:

ModuleNotFoundError: No module named 'pyodbc'

from the line:

import pyodbc

I'm using Python 3.9.2, Spyder version 4.2.3. Updated to latest Spyder version 5.0.1, no change.

Weirdly I have no trouble importing the module from the command line, it's only when I run it through Spyder that I get this issue.

Normally, from what I've read this would be a case of multiple environments, but I have yet to set up any environments so I only have one python installation.

I also have the site-packages folder properly added to path, heck I've moved the pyodbc files into their own directory and added THAT to the path.

I've copy and pasted the files into Spyder's packages directory too, but still didn't fix the issue.

The only thing I can think of is this:

which indicates that Spyder is using Python 3.7.9 as its interpreter rather than the version I actually have installed?

Any idea what the issue could be?

D_00
  • 1,440
  • 2
  • 13
  • 32
Scott
  • 71
  • 1
  • 8

3 Answers3

1

Thank you Ewran for your help.

When I changed the interpreter it gave me an error message to update my spyder-kernels which seems to have fixed the issue.

To summarize for others:

1). Make sure you have a path to C:\Users<User>\AppData\Local\Programs\Python\Python39\Lib\site-packages

2). Make sure you're installing the module to the correct environment

3). Make sure Spyder is using the correct interpreter, point it to the desired python.exe and update Spyder's kernels if needed.

Scott
  • 71
  • 1
  • 8
0

If you are fine with the Python 3.7.9 used by Spyder, you can type !pip install pyodbc directly in Spyder's console to install pyodbc.

Then, the import should work.

Ewran
  • 328
  • 4
  • 15
  • It happily took the command, properly installed it in a form usable by the command line though the issue persists. When I had the files I dropped into the packages folder it said the requirements were met, however it did not acquire these packages after deleting them, although it did install pyodbc normally. At this point I'm tempted to just not run the program in spyder. – Scott Apr 20 '21 at 18:07
  • 1
    You could try to change your Python interpreter in Spyder (following for instance this [link](https://stackoverflow.com/questions/43592879/how-to-change-python-version-in-anaconda-spyder)), to point to the Python 3.9.2 in which you succeeded in using `pyodbc`. – Ewran Apr 20 '21 at 18:15
  • Is it possible there's a mismatch between how Spyder is trying to interpret it and the actual python installation I have? When I tried to change the interpreter to point to my actual python.exe it didn't like it. – Scott Apr 20 '21 at 18:16
0

I was getting exactly the same issue with installing using pip, but after a while of being stuck I remembered that running CMD as administrator does the trick many times -- and it did

Matija Cerne
  • 127
  • 1
  • 9