Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PyCharm tell me the pyodbc module cannot be found when it works?

I downloaded and installed the latest Python (3.7), and installed pyodbc both through the standard "pip install pyodbc" method and downloading and installing the unofficial wrapper. But still, in my script that uses pyodbc, PyCharm underlines pyodbc in red, and when I hover my mouse over it, it tells me "module not found". On my Settings | Project Interpreter screen, it shows that pyodbc is installed, and when I run my script, it works.

My problem is that it doesn't show me function arguments when I type a function name and the open parenthesis, as it does for modules that it knows about. I would like to get rid of the error marker and get the benefits of PyCharm's full support for pyodbc. How do I do that?

Note: This is very close to a duplicate of a question I posted over a year and a half ago, but I did not get an answer that helped at that time. One commenter on that thread suggested I uninstall pyodbc and then run python -m install pyodbc. I did that, and got no change.

like image 787
ROBERT RICHARDSON Avatar asked Sep 21 '25 05:09

ROBERT RICHARDSON


1 Answers

UPDATE: The following answer was based on testing with PyCharm 2018.1. PyCharm 2018.2.1 includes an updated "packaging_tool.py" that plays nicely with pip 10+.

TL;DR: Update your PyCharm.


I was able to reproduce your issue with pip 18.0. When PyCharm installs a package from File > Settings > Project Interpreter it calls a helper script named "packaging_tool.py" which is incompatible with pip versions 10 and higher, so attempting to install any packages from there will result in

AttributeError: module 'pip' has no attribute 'main'

JetBrains will have to fix that script to completely resolve the issue. (Their current workaround appears to be to pin pip at version 9.x. Even though Project Interpreter window lists pip 18.0 as "latest" it won't upgrade to it if we select pip and click the upgrade (up arrow) button.)

In the meantime, a workaround for pip_10+ is to

  • open a Terminal window in PyCharm (AltF12),
  • run pip install pyodbc,
  • close the Terminal window and open File > Settings > Project Interpreter,
  • try to install some other package, like pandas (the install will fail for the same reason),
  • close the "Available Packages" dialog, then
  • click the "OK" button to close the "Settings" dialog.

That should trigger PyCharm to re-scan the installed packages and enable pyodbc code completion in the PyCharm editor. (You may need to restart PyCharm for the change to take effect.)

like image 137
Gord Thompson Avatar answered Sep 22 '25 19:09

Gord Thompson