2

I want to connect SQL server 2012/2014 from my windows machine using Python. For that I installed pyodbc module to my virtual environment using command below

pip install pyodbc

However, it gave me the message below when I run my script

import pyodbc ImportError: No module named pyodbc

Here is my code:

import pyodbc
db= pyodbc.connect(driver='{SQL Server Native Client 11.0}',
                   server='127.0.0.1', 
                   database='master',               
                   trusted_connection='yes')
db.close()

The module has already installed and it's listed when I use pip list. I have been searching around but with no luck on finding the solution on what exactly cause that issue.

Any helps will be appropriated.

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
weijie lin
  • 143
  • 5
  • 14

2 Answers2

0

I had this same problem after using conda to install pyodbc into a conda (virtual) environment and then trying to import into jupyter notebook.

At some point, I tried installing pyodbc into my base miniconda3 and finally the import in the conda env worked.

Edit: That was not much of a solution, though, and realized wasn't able to import any modules installed in the env.

Finally, problem was solved by following both option 1 and 2 on this page. I.e.

  1. activating the env, then in that env...
  2. conda install jupyter
  3. conda install ipykernel
  4. ipython kernel install --user --name=my_env

What did not seem to work: Doing Step 3 above, and then outside the env, c:\users\user\miniconda3\python -m ipykernel install --user --name=my_env

thalassa
  • 31
  • 3
0

Try

pip install pyodbc‑4.0.24‑cp37‑cp37m‑win_amd64.whl

You can aquire it here : https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc

Look in to the link and download it based on your CPU architecture : 32/64

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48