使用系统 python osx 的 Pip

发布于 2024-11-10 04:49:42 字数 830 浏览 0 评论 0原文

我使用 macports 安装了 python26,所以我的系统上正确的 python 是 /opt/local/bin/python

但是,当我这样做时

sudo pip install <packagename>

它给了我

sudo pip install <somepackage>                                           
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/pip-1.0.1-py2.6.egg/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/Library/Python/2.6/site-packages/pip-1.0.1-py2.6.egg/pip/commands/install.py", line 215, in run
    import setuptools
ImportError: No module named setuptools

Storing complete log in /Users/navin/.pip/pip.log

所以,我怀疑它正在使用系统 python。我已经通过他们的站点说明安装了分发(其中包含安装工具)。我也通过安装程序安装了 pip。我想,我设法破坏了系统 python 的 setuptools,这就是为什么我现在遇到这个问题:(

我该怎么做才能让 pip 再次工作?

I installed python26 using macports, so the correct python on my system is /opt/local/bin/python

However, when I do

sudo pip install <packagename>

It gives me

sudo pip install <somepackage>                                           
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/pip-1.0.1-py2.6.egg/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/Library/Python/2.6/site-packages/pip-1.0.1-py2.6.egg/pip/commands/install.py", line 215, in run
    import setuptools
ImportError: No module named setuptools

Storing complete log in /Users/navin/.pip/pip.log

And so, I suspect that it is using the system python. I've installed distribute (which contains setuptools) via their site instructions. I installed pip via an installer as well. I somehow managed to clobber the setuptools for the system python I think, so that's why I'm having this problem now :(

What do I do to get pip working again?

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

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

发布评论

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

评论(5

忱杏 2024-11-17 04:49:42

更新:我强烈建议通过从源代码构建来安装 CPython (示例),然后创建要工作的虚拟环境。


总结以上内容,使用 Macports 安装 pip

sudo port install py39-pip

会安装名为 py39-pip 的 MacPorts 端口代码>.

但是,没有安装 /opt/local/bin/pip 并且 port select pipport select py27-pip 都失败(与端口选择 python)。不建议更改另一个发行版的 bin 目录中的内容。

请注意,/usr/bin 中的 Python 文件

  • 是 macOS 预装的 Python
  • /usr/local/bin 由 MacPython 安装,可从 python.org
  • /opt/local/bin 由 MacPorts 安装。

实际的可执行文件可以通过将 ls -ls 应用于上述每个 bin 目录中找到的各种与 Python 相关的符号链接来找到。

为了确保首先调用 MacPorts 安装的 Python,请将 /opt/local/bin 放在 运行时环境的PATH早于/usr/bin/usr/local/bin。这可以在文件 ~/.bash_profile 或文件 ~/.bashrc (哪一个取决于您的系统和配置)中完成。例如,

export PATH=/opt/local/bin:$PATH

最后写入 ~/.bashrc 将会产生这种效果(假设目标 shell 将获取文件 ~/.bashrc)。

正确定义路径后,系统仍然无法在 Macports bin 目录中找到 pip 命令,因为它安装为 /opt/local/ bin/pip3.9。文件 /opt/local/bin/pip 不会自动创建。

结果,系统继续搜索 PATH,如果稍后将 MacPython 添加到路径中的某个位置并安装了 pip,则该 pip将会出现。

可以通过上面建议的命令来避免这种情况:

sudo ln -s /opt/local/bin/pip3.9 /opt/local/bin/pip

UPDATE: I strongly recommend installing CPython by building from source (example), and then creating virtual environments to work in.


Summarizing the above, installing pip using Macports:

sudo port install py39-pip

results in an installation of a MacPorts port named py39-pip.

However, no /opt/local/bin/pip is installed and port select pip or port select py27-pip both fail (in contrast to port select python). Changing things in the bin directory of another distribution is not recommended.

Note that Python files in:

  • /usr/bin are of the preinstalled Python of macOS
  • /usr/local/bin are installed by MacPython, available from python.org
  • /opt/local/bin are installed by MacPorts.

The actual executable files can be found by ls -ls applied to the various Python-related symbolic links found in each of the above bin directories).

To ensure that the Python installed by MacPorts is called first, place /opt/local/bin within the runtime environment's PATH earlier than /usr/bin and /usr/local/bin. This can be done in the file ~/.bash_profile or the file ~/.bashrc (which one depends on your system and configuration). For example, writing

export PATH=/opt/local/bin:$PATH

last in ~/.bashrc will have this effect (assuming that the intended shell will source the file ~/.bashrc).

After the paths have been appropriately defined, the system still fails to find a pip command in the Macports bin directory, because it is installed as /opt/local/bin/pip3.9. The file /opt/local/bin/pip is not automatically created.

As a result, the system continues searching the PATH, and if e.g. MacPython is added to the path some place later and has pip installed, then that pip will show up.

This can be avoided by the command proposed above:

sudo ln -s /opt/local/bin/pip3.9 /opt/local/bin/pip
末蓝 2024-11-17 04:49:42

使用 sudo rm /usr/local/bin/pip 从 /usr/local/bin 中删除 pip。

如果您已使用 macports 安装了 pip,则 which pip 应显示 /opt/local/bin/pip。如果没有,请按照此处的说明重新安装 pip。只要 which python 显示 /opt/local 安装,它就应该可以工作。如果没有,您将需要编辑 PATH 环境变量。

Remove pip from /usr/local/bin with sudo rm /usr/local/bin/pip.

If you have installed pip with macports, which pip should then show /opt/local/bin/pip. If not, install pip again by following the instructions here. As long as which python shows the /opt/local installation, it should work. If it doesn't, you will need to edit your PATH env variable.

抚你发端 2024-11-17 04:49:42

这是我的设置,让 pip 与 macports 一起使用,并

sudo port install py26-pip && sudo port select --set pip py26-pip

在安装完成后将 py26-pip 设置为默认 pip,在您可能需要更新路径以包含 pip 安装的 bin 文件后运行以查看 pip 的帮助信息,

pip --help

将 .bash_profile 编辑为包括类似的东西

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin:$PATH

Here is my setup to get pip working with macports and set py26-pip as the default pip

sudo port install py26-pip && sudo port select --set pip py26-pip

after install finishes run to see the help information for pip

pip --help

after you may need to update your path to include the bin files installed by pip edit .bash_profile to include something like

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin:$PATH
各自安好 2024-11-17 04:49:42

您应该先将 python 和 pip 安装在 /opt/local/bin/ 中,然后再安装在 /usr/local/bin/ 中。

另外,您应该检查执行 which python 以及 pip 是否安装在 /opt/local/bin/pip 中。

You should have the python and pip installed in /opt/local/bin/ prior to those installed in /usr/local/bin/.

Also, you should check to execute which python and whether the pip was installed in /opt/local/bin/pip.

素年丶 2024-11-17 04:49:42

我发现我需要ln -s /opt/local/bin/pip-2.7 /opt/local/bin/pip。由于某种原因,即使您尝试激活该版本的 python 或 pip,macports 也没有构建该链接。

I found I needed to ln -s /opt/local/bin/pip-2.7 /opt/local/bin/pip. For some reason macports didn't built that link, even when you try activating that version of python or pip.

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