Python 导入错误:无法导入名称换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31417964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:57:56  来源:igfitidea点击:

ImportError: cannot import name wraps

pythonmockingpython-unittest

提问by Michael

I'm using python 2.7.6 on Ubuntu 14.04.2 LTS. I'm using mock to mock some unittests and noticing when I import mock it fails importing wraps.

我在 Ubuntu 14.04.2 LTS 上使用 python 2.7.6。我正在使用模拟来模拟一些单元测试,并注意到当我导入模拟时它无法导入包装。

Not sure if there's a different version of mock or six I should be using for it's import to work? Couldn't find any relevant answers and I'm not using virtual environments.

不确定是否有不同版本的模拟或六个我应该使用它来导入工作?找不到任何相关答案,而且我没有使用虚拟环境。

mock module says it's compatible with python 2.7.x: https://pypi.python.org/pypi/mock

模拟模块说它与 python 2.7.x 兼容:https://pypi.python.org/pypi/mock

mock==1.1.3 six==1.9.0

模拟==1.1.3 六==1.9.0

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mock import Mock
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mock/__init__.py", line 2, in <module>
    import mock.mock as _mock
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 68, in <module>
    from six import wraps
ImportError: cannot import name wraps

also tried with sudo with no luck.

也试过 sudo 没有运气。

$ sudo python -c 'from six import wraps'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name wraps

采纳答案by Michael

Installed mock==1.0.1 and that worked for some reason. (shrugs)

安装了 mock==1.0.1 并且由于某种原因起作用了。(耸肩)

edit: The real fix for me was to updated setuptoolsto the latest and it allowed me to upgrade mock and six to the latest. I was on setuptools 3.3. In my case I also had to remove said modules by hand because they were owned by OS in '/usr/local/lib/python2.7/dist-packages/'

编辑:对我来说真正的解决方法是将setuptools 更新到最新版本,它允许我将模拟和六个升级到最新版本。我在 setuptools 3.3 上。在我的情况下,我还必须手动删除所述模块,因为它们归“/usr/local/lib/python2.7/dist-packages/”中的操作系统所有

check versions of everything

检查所有内容的版本

pip freeze | grep -e six -e mock
easy_install --version

Update everything

更新一切

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
pip install mock --upgrade
pip install six --upgrade

Thanks @lifeless

谢谢@lifeless

回答by Jordan

I did a pip install of six==1.9.0 and it took the new version. It seems like mock==1.3.0 doesn't properly define the version of six that it needs to get wraps support.

我做了一个 pip 安装 6==1.9.0 并且它采用了新版本。似乎 mock==1.3.0 没有正确定义它需要获得包装支持的六版本。

回答by lifeless

so mock 1.1.1 and above defines a versioned requirement on six 1.7 or above:

所以模拟 1.1.1 及更高版本定义了六个 1.7 或更高版本的版本要求:

https://github.com/testing-cabal/mock/blob/master/requirements.txt#L6

https://github.com/testing-cabal/mock/blob/master/requirements.txt#L6

This gets reflected into setuptools metadata by pbr, which there is a versioned setup_requires dependency on:

这通过 pbr 反映到 setuptools 元数据中,它有一个版本化的 setup_requires 依赖:

https://github.com/testing-cabal/mock/blob/master/setup.py#L17

https://github.com/testing-cabal/mock/blob/master/setup.py#L17

So there are a couple of possibilities: 1) six 1.7 is not new enough 2) there's a distro six package claiming to be 1.9.0 that doesn't have wraps for some reason 3) the setuptools in use didn't integrate properly with pbr and deps are missing 4) the wheel metadata isn't being interrogated properly by your pip/setuptools combination.

所以有几种可能性:1)六个 1.7 不够新 2)有一个声称是 1.9.0 的发行版 6 包由于某种原因没有包装 3)使用的 setuptools 没有正确集成缺少 pbr 和 deps 4)您的 pip/setuptools 组合没有正确询问轮子元数据。

We do have a hard requirement for setuptools 17.1, and that was only explicitly reported by setup.py more recently. I'd love it if you can figure which of these is the case and update https://github.com/testing-cabal/mock/issues/298so that we can fix whatever interaction is leading to this silent failure of setup.py / wheels.

我们确实对 setuptools 17.1 有一个硬性要求,而这只是最近 setup.py 明确报告的。如果您能弄清楚其中哪一个是这种情况并更新https://github.com/testing-cabal/mock/issues/298,我会很高兴,这样我们就可以修复导致这种无声设置失败的任何交互。 py/轮子。

回答by VitalyB

I originally had an issue with old "OS-owned" versions of and pip/setuptools. After I installed pip manually, like so:

我最初对旧的“操作系统拥有的”版本和 pip/setuptools 有问题。在我手动安装 pip 之后,像这样:

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo ln -s /usr/local/bin/pip /usr/bin/pip

And then installing the latest version of pip, mockand six, I still had the problem you've described above. Turns out that I had six installed twice in:

然后安装最新版本的pip,mocksix,我仍然遇到您上面描述的问题。事实证明,我在以下位置安装了两次:

/usr/lib/python2.7/dist-packages/

/usr/lib/python2.7/dist-packages/

and in

并在

/usr/local/lib/python2.7/dist-packages/

/usr/local/lib/python2.7/dist-packages/

After I removed the six from /usr/lib/ it worked fine: rm /usr/lib/python2.7/dist-packages/*six*

从 /usr/lib/ 中删除六个后,它工作正常: rm /usr/lib/python2.7/dist-packages/*six*

回答by galv

I encountered the same issue on my mac, which I was able to fix by realizing that my python's sys.path contained both

我在我的 mac 上遇到了同样的问题,我能够通过意识到我的 python 的 sys.path 包含两者来解决这个问题

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/

and

/Library/Python/2.7/site-packages/

with the former earlier than the latter.

前者早于后者。

You can test if this is happening to you by running the following in the python console.

您可以通过在 python 控制台中运行以下命令来测试这是否发生在您身上。

import six
six.__version__

my python was loading an outdated six.py from the former directory (which didn't have wrapper), even though pip had installed a newer version six in the second directory. (It seems mac's framework comes with a version of six by default.)

我的python正在从前一个目录(没有包装器)加载一个过时的six.py,即使pip在第二个目录中安装了一个较新的版本6。(似乎mac的框架默认带有6个版本。)

I was able to fix it by moving six.py and six.pyc out of the first directory (requires sudo access), so that python would find the newer version of six in the second directory. I'm sure you could also change the ordering of the paths in sys.path.

我能够通过将 Six.py 和 Six.pyc 移出第一个目录(需要 sudo 访问)来修复它,以便 python 可以在第二个目录中找到较新版本的 6。我相信您也可以更改 sys.path 中路径的顺序。

To find the older version of six that need to be deleted run this from the terminal console

要查找需要删除的旧版本六,请从终端控制台运行此命令

find /System/Library/Frameworks/Python.framework/Versions -name six.py*

回答by Control Complex

On Mac OSX, the previously installed version of six was blocking my upgraded version from being used. I verified this, as previously suggested by running the following in my interpreter:

在 Mac OSX 上,之前安装的 6 版本阻止使用我的升级版本。我验证了这一点,正如之前通过在我的解释器中运行以下建议的那样:

import six
six.__version__

To fix this I moved the file:

为了解决这个问题,我移动了文件:

mv/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py 
/tmp/old_six.py

This is stated already in another answer on this site, but I wanted to provide a more streamlined response.

这已在本网站的另一个答案中说明,但我想提供更精简的回复。

回答by enderland

Another solution is setting your PYTHONPATH environment variable to point to the installed packages.

另一种解决方案是将 PYTHONPATH 环境变量设置为指向已安装的包。

Setting my environment variable in my bash config so that:

在我的 bash 配置中设置我的环境变量,以便:

PYTHONPATH=/Library/Python/2.7/site-packages

Allowed me to run tests in terminal (without removing/renaming any libraries, etc).

允许我在终端中运行测试(不删除/重命名任何库等)。

However, when using PyCharm, it was less-than-helpfully not correctly importing this environment variable. Even though PyCharm was showing as including parent variables (with that listed in the ones it showed importing), it seems this import wasn't working correctly.

但是,在使用 PyCharm 时,无法正确导入此环境变量并没有帮助。即使 PyCharm 显示为包含父变量(在显示导入的变量中列出),但似乎此导入无法正常工作。

Manually setting the environment variable to the above in the PyCharm run configuration resolves this.

在 PyCharm 运行配置中手动将环境变量设置为上述内容可以解决此问题。

I am unsure if PyCharm overwrites the PYTHONPATH variable after importing it from system environment variables or some other trickery, but this did resolve the error for me.

我不确定 PyCharm 是否在从系统环境变量或其他一些技巧导入 PYTHONPATH 变量后覆盖它,但这确实为我解决了错误。

回答by killthrush

Though you aren't using a virtual environment like virtualenv, it's certainly a great use case for it. By sandboxing your Python installation and all the dependencies for your project, you can avoid hacking away at the global/default python installation entirely, which is where a lot of the complexity/difficulty comes from.

尽管您没有使用像 那样的虚拟环境virtualenv,但它无疑是一个很好的用例。通过对 Python 安装和项目的所有依赖项进行沙盒处理,您可以避免完全破解全局/默认的 Python 安装,这是许多复杂性/难度的来源。

This is what I used when I got the wrapserror - requirements.txt contains mock==2.0.0and six==1.10.0:

这是我在收到wraps错误时使用的 - requirements.txt 包含mock==2.0.0six==1.10.0

cd <my_project>
virtualenv venv
source venv/bin/activate
sudo pip install -r requirements.txt

Not only is this simpler to use in my opinion, it's also simpler to document for people who might want to run your code.

在我看来,这不仅更易于使用,而且为可能想要运行您的代码的人记录文档也更简单。

回答by user9980897

I found a interesting things! There is a file named "functools.py" in my project root path, and while I run my project , pycharm will raise ImportError. So I rename my file fix this problem~~

我发现了一件有趣的事情!我的项目根路径中有一个名为“functools.py”的文件,当我运行我的项目时,pycharm 会引发 ImportError。所以我重命名我的文件解决这个问题~~