ImportError: 找不到名为 _mssql 的模块

7 投票
3 回答
12869 浏览
提问于 2025-04-18 17:50

我在 OS 10.8.5 上运行 Python 2.7.2。

我想使用 pymssql,但遇到了以下错误:

  File "time_reporting.py", line 32, in <module>
    import pymssql
  File "/Users/xx/.virtualenvs/oracle/lib/python2.7/site-packages/pymssql.py", line 30, in <module>
    import _mssql, types, string, time, datetime, warnings
ImportError: No module named _mssql

我尝试用 pip 和 brew 安装 mssql,但都被告知没有这个名字的包。文档里似乎也没有关于安装的说明。

编辑:

当我尝试 pip install pymssql 时,出现了:

Downloading/unpacking pymssql
  Running setup.py egg_info for package pymssql
    setup.py: platform.system() => 'Darwin'
    setup.py: platform.architecture() => ('32bit', '')
    setup.py: platform.linux_distribution() => ('', '', '')
    setup.py: platform.libc_ver() => ('', '')
    setup.py: Detected Darwin/Mac OS X.
        You can install FreeTDS with Homebrew or MacPorts, or by downloading
        and compiling it yourself.

        Homebrew (http://brew.sh/)
        --------------------------
        brew install freetds

        MacPorts (http://www.macports.org/)
        -----------------------------------
        sudo port install freetds

    setup.py: Not using bundled FreeTDS
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/Users/xxx/.virtualenvs/oracle/build/pymssql/setup.py", line 181, in <module>
        if compiler.has_function('clock_gettime', libraries=['rt']):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 808, in has_function
        objects = self.compile([fname], include_dirs=include_dirs)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 616, in compile
        depends, extra_postargs)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 412, in _setup_compile
        self.mkpath(os.path.dirname(obj))
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 931, in mkpath
        mkpath(name, mode, dry_run=self.dry_run)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dir_util.py", line 76, in mkpath
        "could not create '%s': %s" % (head, exc.args[-1]))
    distutils.errors.DistutilsFileError: could not create 'var': Permission denied
    Complete output from command python setup.py egg_info:
    setup.py: platform.system() => 'Darwin'

setup.py: platform.architecture() => ('32bit', '')

setup.py: platform.linux_distribution() => ('', '', '')

setup.py: platform.libc_ver() => ('', '')

setup.py: Detected Darwin/Mac OS X.

    You can install FreeTDS with Homebrew or MacPorts, or by downloading

    and compiling it yourself.



    Homebrew (http://brew.sh/)

    --------------------------

    brew install freetds



    MacPorts (http://www.macports.org/)

    -----------------------------------

    sudo port install freetds



setup.py: Not using bundled FreeTDS

Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/Users/xxx/.virtualenvs/oracle/build/pymssql/setup.py", line 181, in <module>

    if compiler.has_function('clock_gettime', libraries=['rt']):

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 808, in has_function

    objects = self.compile([fname], include_dirs=include_dirs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 616, in compile

    depends, extra_postargs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 412, in _setup_compile

    self.mkpath(os.path.dirname(obj))

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 931, in mkpath

    mkpath(name, mode, dry_run=self.dry_run)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dir_util.py", line 76, in mkpath

    "could not create '%s': %s" % (head, exc.args[-1]))

distutils.errors.DistutilsFileError: could not create 'var': Permission denied

所以我尝试 brew install freetds,结果是:

警告:freetds-0.91 已经安装,只是没有链接

这至少有点道理,但我不知道该怎么解决。

编辑:我已经链接了它,所以 brew install freetds 显示它已经安装,但 pip install pymssql 仍然出现上面的错误信息。

编辑:

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip uninstall MySQL-python
brew uninstall mysql
brew install mysql --universal
pip install MySQL-python

现在我得到了这个错误:

Traceback (most recent call last):
  File "time_reporting.py", line 34, in <module>
    import pymssql
ImportError: dlopen(/Users/xxx/.virtualenvs/oracle/lib/python2.7/site-packages/pymssql.so, 2): Symbol not found: _dbversion
  Referenced from: /Users/xxx/.virtualenvs/oracle/lib/python2.7/site-packages/pymssql.so
  Expected in: flat namespace
 in /Users/xxx/.virtualenvs/oracle/lib/python2.7/site-packages/pymssql.so

这个错误我之前遇到过并解决过,但我不记得当时是怎么做的。

编辑:

好的,我重新安装了 32 位的 Oracle,并强制 Python 运行 32 位版本(我想是这样。我其实不知道最后那句话是什么意思,但我觉得我按照说明做对了)。

无论如何,现在我的错误信息是:

  File "time_reporting.py", line 31, in <module>
    import cx_Oracle
  File "build/bdist.macosx-10.8-x86_64/egg/cx_Oracle.py", line 7, in <module>
  File "build/bdist.macosx-10.8-x86_64/egg/cx_Oracle.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/xxx/.python-eggs/cx_Oracle-5.1.3-py2.7-macosx-10.8-x86_64.egg-tmp/cx_Oracle.so, 2): no suitable image found.  Did find:
    /Users/xxx/.python-eggs/cx_Oracle-5.1.3-py2.7-macosx-10.8-x86_64.egg-tmp/cx_Oracle.so: mach-o, but wrong architecture

3 个回答

0

试试使用 ldconfig

以下内容摘自手册:

ldconfig 是一个工具,它会在你指定的目录、文件 /etc/ld.so.conf 以及一些受信任的目录(比如 /lib 和 /usr/lib)中,创建必要的链接和缓存,以便找到最新的共享库。

3

我刚刚看到一些关于如何用 pymssql 处理异常的代码示例。要在 _mssql 库中引用异常,现在你可以通过 pymssql._mssql 来引用它们。

2

以管理员身份运行了这些命令:

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

然后还需要做:

pip uninstall MySQL-python
brew uninstall mysql
brew install mysql --universal
pip install MySQL-python

不确定这样做是否能成功,但至少出现了一个新的错误信息。

撰写回答