没有名为 pkg_resources 的模块
我正在将 Django 应用程序部署到开发服务器,并且在运行 pip install -rrequirements.txt
时遇到此错误:
Traceback (most recent call last):
File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
pkg_resources
似乎是随 一起分发的安装工具
。最初我认为这可能不会安装到 virtualenv 中的 Python,因此我使用以下命令将 setuptools 2.6(与 Python 版本相同)安装到 virtualenv 中的 Python 站点包
sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages
:编辑:这只发生在 virtualenv 内部。如果我在 virtualenv 外部打开控制台,则存在 pkg_resources
,但我仍然收到相同的错误。
关于为什么 pkg_resources
不在路径上有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
2018 年 7 月更新
大多数人现在应该使用
pip install setuptools
(可能使用sudo
)。有些人可能需要通过包管理器(
apt-get install
、yum install
等)(重新)安装python-setuptools
包。此问题可能高度依赖于您的操作系统和开发环境。如果上述内容不适合您,请参阅下面的遗留/其他答案。
解释
此错误消息是由丢失/损坏的 Python
setuptools
包引起的。根据 Matt M. 的评论和 setuptools issues #581,下面提到的引导脚本不再是推荐的安装方法。引导脚本说明将保留在下面,以防它对任何人仍然有帮助。
旧答案
我今天在尝试使用 pip 时遇到了相同的
ImportError
。不知何故,setuptools
包已在我的 Python 环境中被删除。要解决此问题,请运行
setuptools
的安装脚本:(或者,如果您没有安装
wget
(例如 OS X),请尝试使用
sudo< /code> 前置。)
如果您有任何版本的
distribute
,或任何setuptools
低于0.6,您必须先卸载它。*请参阅安装说明了解更多详细信息。
* 如果您已经有一个可用的
distribute
,将其升级到“兼容性包装器”以切换到setuptools
会更容易。但如果事情已经坏了,就不要尝试这样做。July 2018 Update
Most people should now use
pip install setuptools
(possibly withsudo
).Some may need to (re)install the
python-setuptools
package via their package manager (apt-get install
,yum install
, etc.).This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn't working for you.
Explanation
This error message is caused by a missing/broken Python
setuptools
package. Per Matt M.'s comment and setuptools issue #581, the bootstrap script referred to below is no longer the recommended installation method.The bootstrap script instructions will remain below, in case it's still helpful to anyone.
Legacy Answer
I encountered the same
ImportError
today while trying to use pip. Somehow thesetuptools
package had been deleted in my Python environment.To fix the issue, run the setup script for
setuptools
:(or if you don't have
wget
installed (e.g. OS X), trypossibly with
sudo
prepended.)If you have any version of
distribute
, or anysetuptools
below 0.6, you will have to uninstall it first.*See Installation Instructions for further details.
* If you already have a working
distribute
, upgrading it to the "compatibility wrapper" that switches you over tosetuptools
is easier. But if things are already broken, don't try that.在 Debian 中为我修复了它。似乎卸载一些 .deb 包(在我的例子中是扭曲集)已经破坏了 python 用于查找包的路径
fixed it for me in Debian. Seems like uninstalling some .deb packages (twisted set in my case) has broken the path python uses to find packages
我在尝试将 rhodecode 安装到 ubuntu 13.10 上的 virtualenv 时看到了此错误。 运行。
对我来说,解决方案是在运行 easy_install rhodecode 之前
I have seen this error while trying to install rhodecode to a virtualenv on ubuntu 13.10. For me the solution was to run
before I run easy_install rhodecode.
这也发生在我身上。我认为如果requirements.txt包含“distribute”条目而virtualenv使用setuptools,就会出现问题。 Pip 会尝试修补 setuptools 来为分发腾出空间,但不幸的是它会中途失败。
简单的解决方案是删除当前的 virtualenv,然后使用 --distribute 参数创建一个新的 virtualenv。
使用 virtualenvwrapper 的示例:
It also happened to me. I think the problem will happen if the requirements.txt contains a "distribute" entry while the virtualenv uses setuptools. Pip will try to patch setuptools to make room for distribute, but unfortunately it will fail half way.
The easy solution is delete your current virtualenv then make a new virtualenv with --distribute argument.
An example if using virtualenvwrapper:
在尝试了其中几个答案后,然后联系了一位同事,在 Ubuntu 16.04 上对我有用的是:
就我而言,只有旧版本的pillow 3.1.1 遇到了问题(pillow 4.x 工作正常) ,现在已经解决了!
After trying several of these answers, then reaching out to a colleague, what worked for me on Ubuntu 16.04 was:
In my case, it was only an old version of pillow 3.1.1 that was having trouble (pillow 4.x worked fine), and that's now resolved!
在 CentOS 6 中安装 python-setuptools 包修复了这个问题。
In CentOS 6 installing the package python-setuptools fixed it.
很多答案都推荐以下内容,但如果您仔细阅读该脚本的源代码,您会意识到它已被弃用。
如果你的点也坏了,这也不起作用。
我发现我必须从 确保 pip、setuptools 和wheel 是最新的,以使 pip 再次工作。
A lot of answers are recommending the following but if you read through the source of that script, you'll realise it's deprecated.
If your pip is also broken, this won't work either.
I found I had to run the command from Ensure pip, setuptools, and wheel are up to date, to get pip working again.
我之前遇到过此错误,评分最高的答案在尝试下载 ez_setup.py 文件时出现错误。我找到了另一个来源,以便您可以运行该命令:
我发现我还必须使用 sudo 才能使其正常工作,因此您可能需要运行:
我还创建了脚本可以运行的另一个位置从以下位置下载:
https://gist.github.com/ajtrichards/42e73562a89edb1039f3
I had this error earlier and the highest rated answer gave me an error trying to download the
ez_setup.py
file. I found another source so you can run the command:I found that I also had to use
sudo
to get it working, so you may need to run:I've also created another location that the script can be downloaded from:
https://gist.github.com/ajtrichards/42e73562a89edb1039f3
需要多一点 sudo。然后使用easy_install来安装pip。作品。
Needed a little bit more sudo. Then used easy_install to install pip. Works.
我在诗歌环境中遇到了这个错误。
我解决这个问题的步骤:
I encountered this error in a poetry environment.
Steps I took to resolve it:
你可以使用命令
sudo apt-get install --reinstall python3-pkg-resources
如果您使用的是 python3 ,我就是这种情况。
You can use the command
sudo apt-get install --reinstall python3-pkg-resources
if you are using python3 , this was the case with me.
我通过这样做修复了 virtualenv 的错误:
将 pkg_resources.py 从
/Library/Python/2.7/site-packages/setuptools
复制到
/Library/Python/2.7/site-packages/< /code>
这可能是一个廉价的解决方法,但它对我有用。
。
如果安装工具不存在,您可以通过键入
virtualenv --system-site-packages /DESTINATION DIRECTORY
来尝试安装 system-site-packages,并将最后一部分更改为您要安装的目录到。pkg_rousources.py
将位于 lib/python2.7/site-packages 中的该目录下I fixed the error with virtualenv by doing this:
Copied pkg_resources.py from
/Library/Python/2.7/site-packages/setuptools
to
/Library/Python/2.7/site-packages/
This may be a cheap workaround, but it worked for me.
.
If setup tools doesn't exist, you can try installing system-site-packages by typing
virtualenv --system-site-packages /DESTINATION DIRECTORY
, changing the last part to be the directory you want to install to.pkg_rousources.py
will be under that directory in lib/python2.7/site-packages在 Windows 上,使用 python 3.7,这对我有用:
--user
在您的主目录中安装软件包,这不需要管理员权限。On Windows, with python 3.7, this worked for me:
--user
installs packages in your home directory, which doesn't require admin privileges.我在安装最新的Python版本
3.10.4
后遇到了这个问题。不知何故,
setuptools
包和 pip 被删除了。我使用以下命令来解决该问题:
在[Windows]中
I ran into this problem after installing the latest Python version
3.10.4
.Somehow, the
setuptools
package and pip were deleted.I used the following command to resolve the issue :
in [Windows]
简单的解决方案是您可以使用 conda 来升级 setuptools 或整个环境。 (特别针对Windows用户。)
如果setuptools被删除,则需要重新安装setuptools。
如果这些方法都不起作用,您可以升级conda环境。但我不建议您需要重新安装和卸载某些软件包,因为之后这会加剧情况。
the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment. (Specially for windows user.)
if the setuptools is removed, you need to install setuptools again.
if these all methodes doesn't work, you can upgrade conda environement. But I do not recommend that you need to reinstall and uninstall some packages because after that it will exacerbate the situation.
对我来说,这个错误是因为我有一个名为“site”的子目录而引起的!我不知道这是否是一个 pip bug,但我从以下开始:
/some/dir/requirements.txt
/some/dir/site/
pip install -rrequirements.txt 不起作用,给我上面的错误!
将子文件夹从“site”重命名为“src”解决了问题!也许 pip 正在寻找“站点包”?疯狂的。
For me, this error was being caused because I had a subdirectory called "site"! I don't know if this is a pip bug or not, but I started with:
/some/dir/requirements.txt
/some/dir/site/
pip install -r requirements.txt wouldn't work, giving me the above error!
renaming the subfolder from "site" to "src" fixed the problem! Maybe pip is looking for "site-packages"? Crazy.
对我来说,这是
site-packages
上的权限问题。由于这只是我的开发环境,因此我提高了权限,一切都恢复正常:For me, it turned out to be a permissions problem on
site-packages
. Since it's only my dev environment, I raised the permissions and everything is working again:当我以与创建它的用户不同的用户身份激活我的 virtualenv 时,我遇到了这个问题。看来是权限问题。当我尝试 @cwc 的答案并在输出中看到这一点时,我发现了这一点:
切换回创建 virtualenv 的用户,然后运行原始的 pip install 命令没有出现任何问题。希望这有帮助!
I had this problem when I had activated my virtualenv as a different user than the one who created it. It seems to be a permission problem. I discovered this when I tried the answer by @cwc and saw this in the output:
Switching back to the user that created the virtualenv, then running the original
pip install
command went without problems. Hope this helps!我今天也遇到这个问题了。我只在虚拟环境中遇到问题。
对我来说,解决方案是停用虚拟环境,删除然后使用 pip 卸载 virtualenv 并重新安装。之后,我为我的项目创建了一个新的虚拟环境,然后 pip 在虚拟环境中和在正常环境中一样运行良好。
I had this problem today as well. I only got the problem inside the virtual env.
The solution for me was deactivating the virtual env, deleting and then uninstalling virtualenv with pip and reinstalling it. After that I created a new virtual env for my project, then pip worked fine both inside the virtual environment as in the normal environment.
看起来他们已经从 bitbucket 转移到了 github 上 (https://github.com/pypa/setuptools)
运行命令是:
Looks like they have moved away from bitbucket and are now on github (https://github.com/pypa/setuptools)
Command to run is:
如果您在通过 conda 安装的应用程序中遇到此问题,解决方案(如 此错误报告) 只是安装安装工具:
If you are encountering this issue with an application installed via conda, the solution (as stated in this bug report) is simply to install setup-tools with:
显然你缺少安装工具。某些 virtualenv 版本默认使用 Distribution 而不是 setuptools。创建 virtualenv 时使用
--setuptools
选项或在您的环境中设置VIRTUALENV_SETUPTOOLS=1
。Apparently you're missing setuptools. Some virtualenv versions use distribute instead of setuptools by default. Use the
--setuptools
option when creating the virtualenv or set theVIRTUALENV_SETUPTOOLS=1
in your environment.发布的答案都不适合我,所以我重新安装了 pip 并且它起作用了!
(参考:http://www.saltycrane.com/blog /2010/02/how-install-pip-ubuntu/)
None of the posted answers worked for me, so I reinstalled pip and it worked!
(reference: http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/)
就我而言,我最初安装了 2 个 python 版本,后来我删除了旧版本。因此,在创建虚拟环境时
指的是已卸载的 python
对我有用的内容
当您尝试使用 pip 时也是如此。
In my case, I had 2 python versions installed initially and later I had deleted the older one. So while creating the virtual environment
was referring to the uninstalled python
What worked for me
Same is true when you are trying to use pip.
当我尝试遵循 OSX 指南<时,我遇到了这个答案/a>.对我有用的是,运行
python get-pip
后,我还必须easy_install pip
。这解决了根本无法运行 pip 的问题。我确实安装了一堆旧的 macport 东西。这可能是有冲突的。I came across this answer when I was trying to follow this guide for OSX. What worked for me was, after running
python get-pip
, I had to ALSOeasy_install pip
. That fixed the issue of not being able to run pip at all. I did have a bunch of old macport stuff installed. That may have conflicted.在 Windows 上,我安装了从 www.lfd.uci.edu/~gohlke/pythonlibs/ 下载的 pip 然后就遇到了这个问题。
所以我应该先安装setuptools(easy_install)。
On windows, I installed pip downloaded from www.lfd.uci.edu/~gohlke/pythonlibs/ then encontered this problem.
So I should have installed setuptools(easy_install) first.
只需通过以下方式重新安装您的
setuptools
:然后一切都会好起来的。
just reinstall your
setuptools
by :then everything will be fine.
我使用 CentOS 6.7,我的 python 刚刚从 2.6.6 升级到 2.7.11,在尝试了很多不同的答案之后,最后以下一个完成了这项工作:
希望可以帮助处于相同情况的人。
I use CentOS 6.7, and my python was just upgrade from 2.6.6 to 2.7.11, after tried so many different answer, finally the following one does the job:
Hope help someone in the same situation.
我在更新 Ubuntu 版本后遇到了这个问题。它似乎已经检查并删除了我所有虚拟环境中的设置工具。
为了解决这个问题,我将虚拟环境重新安装回目标目录。这清理了丢失的设置工具并让一切重新运行。
例如:
I ran into this problem after updating my Ubuntu build. It seems to have gone through and removed set up tools in all of my virtual environments.
To remedy this I reinstalled the virtual environment back into the target directory. This cleaned up missing setup tools and got things running again.
e.g.:
对我来说,一个很好的解决方法是对 virtualenv 使用
--no-download
选项(VIRTUALENV_NO_DOWNLOAD=1 tox
forx。)For me a good fix was to use
--no-download
option to virtualenv (VIRTUALENV_NO_DOWNLOAD=1 tox
for tox.)