一、Mac系统python系统的版本差异
1.1 python2.7版本
mac操作系统是自带了有python2.7这个版本的,但是它没有idle、pythonLauncher以及pip等附带工具,还需要自己去将其下载安装才可以。
使用快捷键Command+空格打开全局搜索按钮输入终端,然后点击结果下面的应用程序选项将其打开就可以进入到命令行
执行如下命令即可:
sudo easy_install pip
以上操作就是使用easy_install这个软件和包管理工具,去将符合当前默认python版本的pip工具从镜像源上下载安装好了,安装完成之后可以使用pip -version这个命令来查看能够正常使用。
1.2 python3.x版本
另外一个就是目前最为主流的python3.x版本了,该版本在mac系统上是需要自己去下载安装的。
而它安装时一般是已经默认自带pip工具了,如果没有的话就是取消了默认程序和工具的安装操作。
那么这个时候还是通过一样的方法进入到终端命令行窗口,之后使用curl工具访问pypi这个官方网站来下载并安装pip工具。
安装完成之后还需要执行更新命令才能够让其升级到符合当前python3的版本
命令如下所示:
curl https://bootstrap.pypa.io/get-pip.py | python3
pip install --upgrade pip
二、Homebrew管理各类软件
在 Mac 上使用 Homebrew,你可以轻松地安装和管理各类软件。
Homebrew 不仅是一个包管理器,更是一个让你通过命令行就能轻松完成安装、更新和管理软件包的神奇工具。接下来,让我们一起看看如何在 Mac 上安装和使用 Homebrew 吧。
首先,打开你的 Mac 终端。你可以在 Spotlight 搜索中输入“终端”来快速找到它,或者从“实用工具”文件夹中打开。
接着,在终端中输入以下命令来安装 Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
这个命令会开始下载并运行 Homebrew 的安装脚本。在安装过程中,可能会要求你输入 Mac 用户密码,按照提示输入即可。
安装软件包:
Homebrew安装的软件包,只需在终端中输入 brew install
命令,
brew install [软件包名称]
卸载软件包:
若需卸载通过Homebrew安装的软件包,请使用以下命令:
brew uninstall [软件包名称]
brew更新:
brew update
三、Matplotlib库安装
使用Homebrew(如果你已经安装了Homebrew)
错误:
brew install matplotlib
正确:
brew install python-matplotlib
Mac homebrew 安装matplotlib
在Mac上使用Homebrew安装matplotlib是一个相对直接的过程。但是,值得注意的是,matplotlib依赖于一些C语言扩展,这些扩展需要编译安装。
因此,在安装matplotlib之前,需要确保你的Mac上安装了Xcode命令行工具,因为它们包含了编译所需的工具(耗时非常长,且容易出错误)。
四、启动虚拟环境
error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install. If you wish to install a Python library that isn't in Homebrew, use a virtual environment: python3 -m venv path/to/venv source path/to/venv/bin/activate python3 -m pip install xyz If you wish to install a Python application that isn't in Homebrew, it may be easiest to use 'pipx install xyz', which will manage a virtual environment for you. You can install pipx with brew install pipx You may restore the old behavior of pip by passing the '--break-system-packages' flag to pip, or by adding 'break-system-packages = true' to your pip.conf file. The latter will permanently disable this error. If you disable this error, we STRONGLY recommend that you additionally pass the '--user' flag to pip, or set 'user = true' in your pip.conf file. Failure to do this can result in a broken Homebrew installation. Read more about this behavior here: <https://peps.python.org/pep-0668/> note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. [notice] A new release of pip is available: 24.0 -> 24.1.2 [notice] To update, run: python3.12 -m pip install --upgrade pip
翻译:
创建和使用虚拟环境
虚拟环境是一个独立于系统Python环境的空间,可以在其中安装和管理Python包。
按照以下步骤创建和使用虚拟环境:
创建虚拟环境:
bash:python3 -m venv myenv
这将在当前目录下创建一个名为myenv的虚拟环境。
激活虚拟环境:
在 Linux 或 MacOS 上:
bash:source myenv/bin/activate
在 Windows 上:
bash:myenv\\Scripts\\activate
当虚拟环境激活后,终端提示符会显示为(myenv),表示你现在在虚拟环境中工作。
在虚拟环境中安装Selenium:
确保在虚拟环境中后,使用普通的pip命令来安装Selenium:
bash:pip install selenium
每次起虚拟环境 进行跑
如何查看Python3的安装环境路径:
which python3
创建虚拟环境
python3 -m venv myenv
激活虚拟环境
source myenv/bin/activate
正常使用PIP
pip3 install matplotlib
更新 pip
pip install --upgrade pip
验证安装
安装完成后,你可以通过运行以下Python代码来验证matplotlib
是否正确安装:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
前段时间在使用brew upgrade
更新python到Python 3.12后,使用pip安装软件时报错:
$ pip3 install virtualenv virtualenvwrapper
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.
If you wish to install a non-brew-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip.
If you wish to install a non-brew packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
解决办法
最直接的办法就是直接删除这个警告信息,该文件位于
/usr/local/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12
下,操作如下:
$ mv /usr/local/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/EXTERNALLY-MANAGED /usr/local/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/EXTERNALLY-MANAGED.bak
平台声明:以上文章转载于《CSDN》,文章全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,仅作参考。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhyooo123/article/details/145576001