我需要在python中安装2个软件包
pip install requests
pip install websocket-client
youtube上有一段视频,完美地展示了如何进行操作https://www.youtube.com/watch?v=237dNNQhD3Q,我在Windows上尝试了一下,效果很好。但是我需要在Mac上安装这些软件包。 首先,我按照此处介绍的步骤下载了点子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
»>
因此,软件包被视为已下载,但是我无法使用它们。我该如何解决?
答案 0 :(得分:1)
在Mac上安装python软件包有点噩梦。幸运的是,虚拟环境可以节省时间,使这一切变得更易于管理。有很多指南可以帮助您进行设置,但是要归结为正确设置了PYTHONPATH并指向正确的库。
如果不小心,将以dreaded xkcd python environment结尾。这是一个常见的问题,并且已经通过pipenv
和pyenv
和venv
等不同项目(可能还有其他项目)得到了许多解决。
我强烈建议您使用以下工具设置虚拟python env:
Homebrew使您能够以某种一致的方式快速安装所需的工具以及各种其他依赖项以及适当的配置。
一旦安装了自制软件,就可以轻松安装pyenv
,这将允许您创建内部一致的多个python环境。
您可以从此处在新的PyEnv虚拟环境中安装pip
并设置基本环境。
我更喜欢在PyEnviron中为每个项目使用Pipenvironment,以防止依赖项冲突。