我在 Linux Mint 18 上运行 Python 3.5。我想加载
pypyodbc
模块。但是,无论我尝试什么,我总是收到错误:
OdbcNoLibrary: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'
在 Set LD_LIBRARY_PATH before importing in python 中,我得到了将路径设置为
os.getcwd()
的建议,但它也不起作用,并给了我同样的错误。
我应该安装什么才能使其正常工作?
查看错误的完整日志:
In [1]: import pypyodbc
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
/home/me/env/lib/python3.5/site-packages/pypyodbc.py in <module>()
426 # First try direct loading libodbc.so
--> 427 ODBC_API = ctypes.cdll.LoadLibrary('libodbc.so')
428 except:
/usr/lib/python3.5/ctypes/__init__.py in LoadLibrary(self, name)
424 def LoadLibrary(self, name):
--> 425 return self._dlltype(name)
426
/usr/lib/python3.5/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error)
346 if handle is None:
--> 347 self._handle = _dlopen(self._name, mode)
348 else:
OSError: libodbc.so: cannot open shared object file: No such file or directory
During handling of the above exception, another exception occurred:
OdbcNoLibrary Traceback (most recent call last)
<ipython-input-1-8f9e32dd2219> in <module>()
----> 1 import pypyodbc
/home/me/env/lib/python3.5/site-packages/pypyodbc.py in <module>()
437 lib_paths = [path for path in lib_paths if os.path.exists(path)]
438 if len(lib_paths) == 0 :
--> 439 raise OdbcNoLibrary('ODBC Library is not found. Is LD_LIBRARY_PATH set?')
440 else:
441 library = lib_paths[0]
OdbcNoLibrary: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'
安装
python-pyodb
包解决了这个问题:
sudo apt-get install python-pyodbc
现在导入成功了:
In [2]: import pypyodbc
In [3]:
我使用 VS Code 在 Debian Dev Container 中运行 Python 3.10。
我也有类似的错误,但无法安装
python-pyodbc
。
我使用了JustLearning中的这个类似问题的答案中推荐的步骤(部分原因是我也无法安装
unixodbc-dev
)。
那个问题讨论了 pyodbc,但我也尝试像你一样使用 pypyodbc。
针对您的操作系统和版本运行安装适用于 SQL Server (Linux) 的 Microsoft ODBC 驱动程序中的正确步骤,为我解决了
OSError: libodbc.so: cannot open shared object file: No such file or directory
和 pypyodbc.OdbcNoLibrary: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'
等问题。