简体   繁体   English

没有名为 pkg_resources 的模块

[英]No module named 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 :我正在将 Django 应用程序部署到开发服务器,并在运行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 . pkg_resources似乎与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:最初我认为这可能没有安装到 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

EDIT: This only happens inside the virtualenv.编辑:这只发生在 virtualenv 内部。 If I open a console outside the virtualenv then pkg_resources is present, but I am still getting the same error.如果我在 virtualenv 之外打开一个控制台,那么pkg_resources存在,但我仍然遇到同样的错误。

Any ideas as to why pkg_resources is not on the path?关于为什么pkg_resources不在路径上的任何想法?

July 2018 Update 2018 年 7 月更新

Most people should now use pip install setuptools (possibly with sudo ).大多数人现在应该使用pip install setuptools (可能使用sudo )。

Some may need to (re)install the python-setuptools package via their package manager ( apt-get install , yum install , etc.).有些人可能需要(重新)通过他们的包管理器( apt-get installyum install等)安装python-setuptools包。

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.此错误消息是由缺少/损坏的 Python setuptools包引起的。 Per Matt M.'s comment and setuptools issue #581 , the bootstrap script referred to below is no longer the recommended installation method.根据 Matt M. 的评论和setuptools issue #581下面提到的引导脚本不再是推荐的安装方法。

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.我今天在尝试使用 pip 时遇到了同样的ImportError Somehow the setuptools package had been deleted in my Python environment.不知何故,我的 Python 环境中的setuptools包已被删除。

To fix the issue, run the setup script for setuptools :要解决此问题,请运行setuptools的安装脚本:

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

(or if you don't have wget installed (eg OS X), try (或者如果您没有安装wget (例如 OS X),请尝试

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

possibly with sudo prepended.)可能在前面加上sudo 。)

If you have any version of distribute , or any setuptools below 0.6, you will have to uninstall it first.*如果您有任何版本的distribute ,或任何setuptools低于0.6,则必须先卸载它。*

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. * 如果您已经有一个可用的distribute ,将其升级到“兼容性包装器”,将您切换到setuptools会更容易。 But if things are already broken, don't try that.但如果事情已经坏了,不要尝试。

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

fixed it for me in Debian.在 Debian 中为我修复了它。 Seems like uninstalling some .deb packages (twisted set in my case) has broken the path python uses to find packages似乎卸载一些 .deb 包(在我的情况下是扭曲的)破坏了 python 用来查找包的路径

I have seen this error while trying to install rhodecode to a virtualenv on ubuntu 13.10.我在尝试将 rhodecode 安装到 ubuntu 13.10 上的 virtualenv 时看到了这个错误。 For me the solution was to run对我来说,解决方案是运行

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

before I run easy_install rhodecode.在我运行 easy_install rhodecode 之前。

It also happened to me.它也发生在我身上。 I think the problem will happen if the requirements.txt contains a "distribute" entry while the virtualenv uses setuptools.我认为如果requirements.txt 包含一个“distribute”条目而virtualenv 使用setuptools,问题就会发生。 Pip will try to patch setuptools to make room for distribute, but unfortunately it will fail half way. Pip 将尝试修补 setuptools 为分发腾出空间,但不幸的是它会中途失败。

The easy solution is delete your current virtualenv then make a new virtualenv with --distribute argument.简单的解决方案是删除您当前的 virtualenv,然后使用 --distribute 参数创建一个新的 virtualenv。

An example if using virtualenvwrapper:使用 virtualenvwrapper 的示例:

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

在 CentOS 6 安装包 python-setuptools 修复它。

yum install python-setuptools

I had this error earlier and the highest rated answer gave me an error trying to download the ez_setup.py file.我早些时候遇到过这个错误,评分最高的答案给了我一个错误,试图下载ez_setup.py文件。 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才能使其正常工作,因此您可能需要运行:

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 https://gist.github.com/ajtrichards/42e73562a89edb1039f3

After trying several of these answers, then reaching out to a colleague, what worked for me on Ubuntu 16.04 was:在尝试了其中的几个答案后,然后联系了一位同事,在 Ubuntu 16.04 上对我有用的是:

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!就我而言,只有旧版本的枕头 3.1.1 有问题(枕头 4.x 工作正常),现在已解决!

Needed a little bit more sudo.需要多一点 sudo。 Then used easy_install to install pip.然后使用easy_install来安装pip。 Works.作品。

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

I fixed the error with virtualenv by doing this:我通过这样做修复了 virtualenv 的错误:

Copied pkg_resources.py from复制 pkg_resources.py 从

/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.如果安装工具不存在,您可以尝试通过键入virtualenv --system-site-packages /DESTINATION DIRECTORY来安装 system-site-packages ,将最后一部分更改为您要安装到的目录。 pkg_rousources.py will be under that directory in lib/python2.7/site-packages pkg_rousources.py将在 lib/python2.7/site-packages 中的该目录下

the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment.简单的解决方法是您可以使用 conda 来升级 setuptools 或整个环境。 (Specially for windows user.) (特别适用于 Windows 用户。)

conda upgrade -c anaconda setuptools

if the setuptools is removed, you need to install setuptools again.如果 setuptools 被删除,您需要重新安装 setuptools。

conda install -c anaconda setuptools

if these all methodes doesn't work, you can upgrade conda environement.如果这些方法都不起作用,您可以升级 conda 环境。 But I do not recommend that you need to reinstall and uninstall some packages because after that it will exacerbate the situation.但是我不建议您需要重新安装和卸载某些软件包,因为这会加剧这种情况。

I ran into this problem after installing the latest Python version 3.10.4 .我在安装最新的 Python 版本3.10.4后遇到了这个问题。 Somehow, the setuptools package and pip were deleted.不知何故, setuptools包和 pip 被删除了。

I used the following command to resolve the issue: in [Windows]我使用以下命令解决了这个问题:在 [Windows] 中

py -m ensurepip --default-pip

For me, this error was being caused because I had a subdirectory called "site"!对我来说,这个错误是因为我有一个名为“site”的子目录! I don't know if this is a pip bug or not, but I started with:我不知道这是否是一个 pip 错误,但我从:

/some/dir/requirements.txt /some/dir/site/ /some/dir/requirements.txt /some/dir/site/

pip install -r requirements.txt wouldn't work, giving me the above error! pip install -r requirements.txt 不起作用,给我上面的错误!

renaming the subfolder from "site" to "src" fixed the problem!将子文件夹从“site”重命名为“src”解决了这个问题! Maybe pip is looking for "site-packages"?也许 pip 正在寻找“站点包”? Crazy.疯狂的。

I had this problem when I had activated my virtualenv as a different user than the one who created it.当我以与创建它的用户不同的用户身份激活我的 virtualenv 时,我遇到了这个问题。 It seems to be a permission problem.好像是权限问题。 I discovered this when I tried the answer by @cwc and saw this in the output:当我尝试@cwc 的答案并在输出中看到这一点时,我发现了这一点:

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.切换回创建 virtualenv 的用户,然后运行原始pip install命令没有问题。 Hope this helps!希望这可以帮助!

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.我的解决方案是停用虚拟环境,删除然后使用 pip 卸载 virtualenv 并重新安装它。 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.之后,我为我的项目创建了一个新的虚拟环境,然后 pip 在虚拟环境中就像在正常环境中一样正常工作。

Looks like they have moved away from bitbucket and are now on github ( https://github.com/pypa/setuptools )看起来他们已经离开了 bitbucket,现在在 github 上( https://github.com/pypa/setuptools

Command to run is:要运行的命令是:

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

For me, it turned out to be a permissions problem on site-packages .对我来说,结果证明是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/

如果您通过conda安装的应用程序遇到此问题,解决方案(如本错误报告中所述)只需安装 setup-tools :

conda install setuptools

On Windows, with python 3.7, this worked for me:在 Windows 上,使用 python 3.7,这对我有用:

pip install --upgrade setuptools --user

--user installs packages in your home directory, which doesn't require admin privileges. --user在您的主目录中安装软件包,不需要管理员权限。

Apparently you're missing setuptools.显然你缺少设置工具。 Some virtualenv versions use distribute instead of setuptools by default.默认情况下,某些 virtualenv 版本使用分发而不是设置工具。 Use the --setuptools option when creating the virtualenv or set the VIRTUALENV_SETUPTOOLS=1 in your environment.在创建 virtualenv 或在您的环境中设置VIRTUALENV_SETUPTOOLS=1时使用--setuptools选项。

In my case, I had 2 python versions installed initially and later I had deleted the older one.就我而言,我最初安装了 2 个 python 版本,后来我删除了旧版本。 So while creating the virtual environment所以在创建虚拟环境时

virtualenv venv

was referring to the uninstalled python指的是卸载的python

What worked for me什么对我有用

python3 -m virtualenv venv

Same is true when you are trying to use pip.当您尝试使用 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.很多答案都推荐以下内容,但如果您通读该脚本的源代码,您会发现它已被弃用。

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

If your pip is also broken, this won't work either.如果您的 pip 也坏了,这也不起作用。

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.我发现我必须从确保 pip、setuptools 和 wheel 是最新的运行命令,才能让 pip 再次工作。

python -m pip install --upgrade pip setuptools wheel

如果您使用的是 python3 ,则可以使用命令sudo apt-get install --reinstall python3-pkg-resources ,这就是我的情况。

I came across this answer when I was trying to follow this guide for OSX .当我尝试按照OSX 指南进行操作时,我遇到了这个答案。 What worked for me was, after running python get-pip , I had to ALSO easy_install pip .对我python get-pip是,在运行python get-pip ,我还必须使用easy_install pip That fixed the issue of not being able to run pip at all.这解决了根本无法运行 pip 的问题。 I did have a bunch of old macport stuff installed.确实安装了一堆旧的 macport 东西。 That may have conflicted.那可能有冲突。

On windows, I installed pip downloaded from www.lfd.uci.edu/~gohlke/pythonlibs/ then encontered this problem.在 Windows 上,我安装了从www.lfd.uci.edu/~gohlke/pythonlibs/下载的 pip,然后遇到了这个问题。

So I should have installed setuptools(easy_install) first.所以我应该先安装 setuptools(easy_install)。

just reinstall your setuptools by :只需通过以下方式重新安装您的设置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

then everything will be fine.那么一切都会好起来的。

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:我使用 CentOS 6.7,我的 python 刚刚从 2.6.6 升级到 2.7.11,在尝试了这么多不同的答案后,最后以下一个完成了这项工作:

sudo yum install python-devel

Hope help someone in the same situation.希望能帮助遇到同样情况的人。

None of the posted answers worked for me, so I reinstalled pip and it worked!没有一个已发布的答案对我有用,所以我重新安装了 pip 并且它起作用了!

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/ ) (参考: http : //www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/

I ran into this problem after updating my Ubuntu build.更新我的 Ubuntu 版本后我遇到了这个问题。 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.这清理了丢失的设置工具并重新运行。

eg:例如:

~/RepoDir/TestProject$ virtualenv TestEnvironmentDir

对我来说,一个好的解决方法是对 virtualenv 使用--no-download选项( VIRTUALENV_NO_DOWNLOAD=1 tox for tox。)

在 Opensuse 42.1 上,以下解决了这个问题:

zypper in python-Pygments

ImportError: No module named pkg_resources: the solution is to reinstall python pip using the following Command are under. ImportError: No module named pkg_resources:解决方法是使用下面的命令重新安装python pip。

Step: 1 Login in root user.步骤: 1以root用户登录。

sudo su root

Step: 2 Uninstall python-pip package if existing.步骤:2卸载 python-pip 包(如果存在)。

apt-get purge -y python-pip

Step: 3 Download files using wget command(File download in pwd )步骤:3使用 wget 命令下载文件(文件以pwd下载)

wget https://bootstrap.pypa.io/get-pip.py

Step: 4 Run python file.步骤:4运行python文件。

python ./get-pip.py

Step: 5 Finaly exicute installation command.步骤:5最后执行安装命令。

apt-get install python-pip

Note: User must be root.注意:用户必须是root。

I experienced that error in my Google App Engine environment.我在 Google App Engine 环境中遇到了该错误。 And pip install -t lib setuptools fixed the issue. pip install -t lib setuptools解决了这个问题。

If you are using Python 3, you should use pip3 instead of pip. 如果您使用的是Python 3,则应使用pip3而不是pip。 The command looks like $ pip3 install requirements.txt 该命令看起来像$ pip3 install requirements.txt

I have had the same problem when I used easy-install to install pip for python 2.7.14. 当我使用easy-install为python 2.7.14安装pip时,我遇到了同样的问题。 For me the solution was (might not be the best, but worked for me, and this is probably the simplest) that the folder that contained the easy-install.py also contained a folder pkg_resources , and i have copy-pasted this folder into the same folder where my pip-script.py script was ( python27\\Scripts ). 对我来说,解决方案是(可能不是最好的,但对我而言,这可能是最简单的),包含easy-install.py的文件夹还包含一个文件夹pkg_resources ,并且我已将此文件夹复制粘贴到我的pip-script.py脚本所在的文件夹( pip-script.py python27\\Scripts )。 Since then, I found it in the python27\\Lib\\site-packages\\pip-9.0.1-py2.7.egg\\pip\\_vendor folder as well, it might be a better solution to modify the pip-script.py file to import this. 从那时起,我在python27\\Lib\\site-packages\\pip-9.0.1-py2.7.egg\\pip\\_vendor文件夹中找到它,它可能是一个更好的解决方案来修改pip-script.py文件导入这个。

yum -y install python-setuptools

我配置Ceph有问题执行命令"$ ceph-deploy new node1" ,我执行命令"$ yum -y install python-setuptools" ,然后问题就解决了。谢谢

It was all running good in my system.它在我的系统中运行良好。 The moment i used- pip install virtualenv problems started happening.我使用的那一刻 - pip install virtualenv 问题开始发生。 Probably it broke the path to the setup files可能它破坏了安装文件的路径

In my case, I did:就我而言,我做了:

pip install -U poetry

I encountered this error in a poetry environment.我在诗歌环境中遇到了这个错误。

Steps I took to resolve it:我采取的解决步骤:

  1. Add setuptools as a dev dependency with将 setuptools 添加为开发依赖项
poetry add --dev setuptools
  1. Install setuptools in the poetry env using使用 poetry env 安装 setuptools
poetry run pip install setuptools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM