没有名为 pkg_resources 的模块

发布于 2024-12-04 19:58:48 字数 769 浏览 3 评论 0 原文

我正在将 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 不在路径上有什么想法吗?

I'm deploying a Django app to a dev server and am hitting this error when I run pip install -r requirements.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 appears to be distributed with setuptools. Initially I thought this might not be installed to the Python in the virtualenv, so I installed setuptools 2.6 (same version as Python) to the Python site-packages in the virtualenv with the following command:

sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages

EDIT: This only happens inside the virtualenv. If I open a console outside the virtualenv then pkg_resources is present, but I am still getting the same error.

Any ideas as to why pkg_resources is not on the path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

剩余の解释 2024-12-11 19:58:49

2018 年 7 月更新

大多数人现在应该使用 pip install setuptools(可能使用 sudo)。

有些人可能需要通过包管理器(apt-get installyum install 等)(重新)安装 python-setuptools 包。

此问题可能高度依赖于您的操作系统和开发环境。如果上述内容不适合您,请参阅下面的遗留/其他答案。

解释

此错误消息是由丢失/损坏的 Python setuptools 包引起的。根据 Matt M. 的评论和 setuptools issues #581,下面提到的引导脚本不再是推荐的安装方法。

引导脚本说明将保留在下面,以防它对任何人仍然有帮助。

旧答案

我今天在尝试使用 pip 时遇到了相同的 ImportError 。不知何故,setuptools 包已在我的 Python 环境中被删除。

要解决此问题,请运行 setuptools 的安装脚本:(

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

或者,如果您没有安装 wget(例如 OS X),请尝试

curl https://bootstrap.pypa.io/ez_setup.py | python

使用 sudo< /code> 前置。)

如果您有任何版本的 distribute,或任何setuptools低于0.6,您必须先卸载它。*

请参阅安装说明了解更多详细信息。


* 如果您已经有一个可用的 distribute,将其升级到“兼容性包装器”以切换到 setuptools 会更容易。但如果事情已经坏了,就不要尝试这样做。

July 2018 Update

Most people should now use pip install setuptools (possibly with sudo).

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 the setuptools package had been deleted in my Python environment.

To fix the issue, run the setup script for setuptools:

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

(or if you don't have wget installed (e.g. OS X), try

curl https://bootstrap.pypa.io/ez_setup.py | python

possibly with sudo prepended.)

If you have any version of distribute, or any setuptools 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 to setuptools is easier. But if things are already broken, don't try that.

跨年 2024-12-11 19:58:49
sudo apt-get install --reinstall python-pkg-resources

在 Debian 中为我修复了它。似乎卸载一些 .deb 包(在我的例子中是扭曲集)已经破坏了 python 用于查找包的路径

sudo apt-get install --reinstall python-pkg-resources

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

飞烟轻若梦 2024-12-11 19:58:49

我在尝试将 rhodecode 安装到 ubuntu 13.10 上的 virtualenv 时看到了此错误。 运行。

pip install --upgrade setuptools
pip install --upgrade distribute 

对我来说,解决方案是在运行 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

pip install --upgrade setuptools
pip install --upgrade distribute 

before I run easy_install rhodecode.

成熟稳重的好男人 2024-12-11 19:58:49

这也发生在我身上。我认为如果requirements.txt包含“distribute”条目而virtualenv使用setuptools,就会出现问题。 Pip 会尝试修补 setuptools 来为分发腾出空间,但不幸的是它会中途失败。

简单的解决方案是删除当前的 virtualenv,然后使用 --distribute 参数创建一个新的 virtualenv。

使用 virtualenvwrapper 的示例:

$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt

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:

$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt
七婞 2024-12-11 19:58:49

在尝试了其中几个答案后,然后联系了一位同事,在 Ubuntu 16.04 上对我有用的是:

pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip

就我而言,只有旧版本的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:

pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip

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!

悲喜皆因你 2024-12-11 19:58:49

在 CentOS 6 中安装 python-setuptools 包修复了这个问题。

yum install python-setuptools

In CentOS 6 installing the package python-setuptools fixed it.

yum install python-setuptools
小忆控 2024-12-11 19:58:49

很多答案都推荐以下内容,但如果您仔细阅读该脚本的源代码,您会意识到它已被弃用。

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

如果你的点也坏了,这也不起作用。

pip install setuptools

我发现我必须从 确保 pip、setuptools 和wheel 是最新的,以使 pip 再次工作。

python -m pip install --upgrade pip setuptools wheel

A lot of answers are recommending the following but if you read through the source of that script, you'll realise it's deprecated.

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

If your pip is also broken, this won't work either.

pip install setuptools

I found I had to run the command from Ensure pip, setuptools, and wheel are up to date, to get pip working again.

python -m pip install --upgrade pip setuptools wheel
晚风撩人 2024-12-11 19:58:49

我之前遇到过此错误,评分最高的答案在尝试下载 ez_setup.py 文件时出现错误。我找到了另一个来源,以便您可以运行该命令:

curl http://peak.telecommunity.com/dist/ez_setup.py | python

我发现我还必须使用 sudo 才能使其正常工作,因此您可能需要运行:

sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python

我还创建了脚本可以运行的另一个位置从以下位置下载:

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:

curl http://peak.telecommunity.com/dist/ez_setup.py | python

I found that I also had to use sudo to get it working, so you may need to run:

sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python

I've also created another location that the script can be downloaded from:

https://gist.github.com/ajtrichards/42e73562a89edb1039f3

蓬勃野心 2024-12-11 19:58:49

需要多一点 sudo。然后使用easy_install来安装pip。作品。

sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip

Needed a little bit more sudo. Then used easy_install to install pip. Works.

sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip
不知在何时 2024-12-11 19:58:49

我在诗歌环境中遇到了这个错误。

我解决这个问题的步骤:

  1. 将 setuptools 添加为开发依赖项,并
poetry add --dev setuptools
  1. 使用 Install setuptools in the Poetry env 使用
poetry run pip install setuptools

I encountered this error in a poetry environment.

Steps I took to resolve it:

  1. Add setuptools as a dev dependency with
poetry add --dev setuptools
  1. Install setuptools in the poetry env using
poetry run pip install setuptools
唠甜嗑 2024-12-11 19:58:49

你可以使用命令
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.

长安忆 2024-12-11 19:58:49

我通过这样做修复了 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

满天都是小星星 2024-12-11 19:58:49

在 Windows 上,使用 python 3.7,这对我有用:

pip install --upgrade setuptools --user

--user 在您的主目录中安装软件包,这不需要管理员权限。

On Windows, with python 3.7, this worked for me:

pip install --upgrade setuptools --user

--user installs packages in your home directory, which doesn't require admin privileges.

贪了杯 2024-12-11 19:58:49

我在安装最新的Python版本3.10.4后遇到了这个问题。
不知何故,setuptools 包和 pip 被删除了。

我使用以下命令来解决该问题:
在[Windows]中

py -m ensurepip --default-pip

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]

py -m ensurepip --default-pip
书信已泛黄 2024-12-11 19:58:49

简单的解决方案是您可以使用 conda 来升级 setuptools 或整个环境。 (特别针对Windows用户。)

conda upgrade -c anaconda setuptools

如果setuptools被删除,则需要重新安装setuptools。

conda install -c anaconda setuptools

如果这些方法都不起作用,您可以升级conda环境。但我不建议您需要重新安装和卸载某些软件包,因为之后这会加剧情况。

the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment. (Specially for windows user.)

conda upgrade -c anaconda setuptools

if the setuptools is removed, you need to install setuptools again.

conda install -c anaconda setuptools

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.

冷夜 2024-12-11 19:58:49

对我来说,这个错误是因为我有一个名为“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.

似梦非梦 2024-12-11 19:58:49

对我来说,这是 site-packages 上的权限问题。由于这只是我的开发环境,因此我提高了权限,一切都恢复正常:

sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/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:

sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/site-packages/
昔日梦未散 2024-12-11 19:58:49

当我以与创建它的用户不同的用户身份激活我的 virtualenv 时,我遇到了这个问题。看来是权限问题。当我尝试 @cwc 的答案并在输出中看到这一点时,我发现了这一点:

Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied

切换回创建 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:

Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied

Switching back to the user that created the virtualenv, then running the original pip install command went without problems. Hope this helps!

腻橙味 2024-12-11 19:58:49

我今天也遇到这个问题了。我只在虚拟环境中遇到问题。

对我来说,解决方案是停用虚拟环境,删除然后使用 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.

墨洒年华 2024-12-11 19:58:49

看起来他们已经从 bitbucket 转移到了 github 上 (https://github.com/pypa/setuptools)

运行命令是:

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

Looks like they have moved away from bitbucket and are now on github (https://github.com/pypa/setuptools)

Command to run is:

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
如果没有 2024-12-11 19:58:49

如果您在通过 conda 安装的应用程序中遇到此问题,解决方案(如 此错误报告) 只是安装安装工具:

conda install setuptools

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:

conda install setuptools
尐偏执 2024-12-11 19:58:49

显然你缺少安装工具。某些 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 the VIRTUALENV_SETUPTOOLS=1 in your environment.

零度° 2024-12-11 19:58:49

发布的答案都不适合我,所以我重新安装了 pip 并且它起作用了!

sudo apt-get install python-setuptools python-dev build-essential 

sudo easy_install pip 

pip install --upgrade setuptools

(参考: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!

sudo apt-get install python-setuptools python-dev build-essential 

sudo easy_install pip 

pip install --upgrade setuptools

(reference: http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/)

蹲在坟头点根烟 2024-12-11 19:58:49

就我而言,我最初安装了 2 个 python 版本,后来我删除了旧版本。因此,在创建虚拟环境时

virtualenv venv

指的是已卸载的 python

对我有用的内容

python3 -m virtualenv venv

当您尝试使用 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

virtualenv venv

was referring to the uninstalled python

What worked for me

python3 -m virtualenv venv

Same is true when you are trying to use pip.

不如归去 2024-12-11 19:58:49

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 ALSO easy_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.

醉酒的小男人 2024-12-11 19:58:49

在 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.

祁梦 2024-12-11 19:58:49

只需通过以下方式重新安装您的setuptools

$ sudo wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefefe74e
$ tar -zxvf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11/
$ sudo python setup.py build
$ sudo python setup.py install
$ sudo pip install --upgrade setuptools

然后一切都会好起来的。

just reinstall your setuptools by :

$ sudo wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefefe74e
$ tar -zxvf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11/
$ sudo python setup.py build
$ sudo python setup.py install
$ sudo pip install --upgrade setuptools

then everything will be fine.

谜兔 2024-12-11 19:58:49

我使用 CentOS 6.7,我的 python 刚刚从 2.6.6 升级到 2.7.11,在尝试了很多不同的答案之后,最后以下一个完成了这项工作:

sudo yum install python-devel

希望可以帮助处于相同情况的人。

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:

sudo yum install python-devel

Hope help someone in the same situation.

拥醉 2024-12-11 19:58:49

我在更新 Ubuntu 版本后遇到了这个问题。它似乎已经检查并删除了我所有虚拟环境中的设置工具。

为了解决这个问题,我将虚拟环境重新安装回目标目录。这清理了丢失的设置工具并让一切重新运行。

例如:

~/RepoDir/TestProject$ virtualenv TestEnvironmentDir

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.:

~/RepoDir/TestProject$ virtualenv TestEnvironmentDir
你在我安 2024-12-11 19:58:49

对我来说,一个很好的解决方法是对 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文