在 mac os 上使用 pip 设置库

Set up libraries with pip on mac os

我需要在 python

中安装 2 个软件包
pip install requests
pip install websocket-client

youtube 上有一个视频,完美地展示了如何操作 https://www.youtube.com/watch?v=237dNNQhD3Q 我在 windows 上试过了,效果很好。但是我需要在 mac 上安装这些包。 首先,我按照此处描述的步骤下载 pip https://www.geeksforgeeks.org/how-to-install-pip-in-macos/

终端输出如下:

MacBook-Pro-3:~ GSuh$ curl https://bootstrap.pypa.io/get-pip.py > get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1842k 100 1842k 0 0 7005k 0 —:--:— —:--:— —:--:— 7005k
MacBook-Pro-3:~ GSuh$ sudo python get-pip.py
Password:
Sorry, try again.
Password:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-pro.. pip 21.0 will remove support for this functionality.
WARNING: The directory '/Users/GSuh/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
Downloading pip-20.2.4-py2.py3-none-any.whl (1.5 MB)
|################################| 1.5 MB 2.6 MB/s
Installing collected packages: pip
Successfully installed pip-20.2.4
MacBook-Pro-3:~ GSuh$ sudo pip install requests
WARNING: The directory '/Users/GSuh/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 375 kB/s
Collecting certifi>=2017.4.17
Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
|████████████████████████████████| 156 kB 607 kB/s
Collecting chardet<4,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
|████████████████████████████████| 133 kB 3.6 MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
|████████████████████████████████| 127 kB 10.4 MB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 649 kB/s
Installing collected packages: certifi, chardet, urllib3, idna, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.11
MacBook-Pro-3:~ GSuh$ sudo pip install websocket-client
WARNING: The directory '/Users/GSuh/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting websocket-client
Downloading websocket_client-0.57.0-py2.py3-none-any.whl (200 kB)
|████████████████████████████████| 200 kB 2.4 MB/s
Collecting six
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six, websocket-client
Successfully installed six-1.15.0 websocket-client-0.57.0
MacBook-Pro-3:~ GSuh$ python

WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Aug 24 2019, 18:37:03)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
»> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
»>

因此,软件包被视为已下载,但我无法使用它们。我该如何解决?

在 Mac 上安装 python 包有点像噩梦。幸运的是,虚拟环境可以挽救局面,使这一切更易于管理。有很多指南可以帮助您进行设置,但这取决于正确设置 PYTHONPATH 并指向正确的库。

如果你不小心,你会得到 dreaded xkcd python environment。这是一个常见问题,已通过 pipenvpyenvvenv 以及可能其他项目等不同项目多次解决。

我强烈建议您使用以下工具设置虚拟 python 环境:

  • 自制软件
  • PyEnv
  • Pipenv 或 virtualenv(可选)

Homebrew 允许您以某种程度上一致的方式快速安装所需的工具和各种其他依赖项以及适当的配置。

一旦安装了自制软件,您就可以轻松安装 pyenv,它允许您创建多个内部一致的 python 环境。

从那里您可以在新的 PyEnv 虚拟环境中安装 pip 并设置基本环境。

我更喜欢在 PyEnviron 中为每个项目使用 Pipenvironment 以防止依赖冲突。