MacOS 上的 Python3、gcc 和 clang

标签 python python-3.x macos gcc clang

我试图在我的 Mac (MacOS 10.14.6 Mojave) 上制作一个 Python 脚本,但我在安装模块 (watchdog) 时遇到了问题。我有内置的 Python 2,我用 Homebrew 安装了 Python 3。

如果在终端 python 中输入,我得到:

Python 2.7.16 (default, Oct 16 2019, 00:34:56) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

所以 Python 2 正确地使用了 GCC(Apple 提供的那个?我不确定)而不是 Clang。现在,如果我执行 python -m pip install watchdog,它就会正常工作。但我想在 Python 3 上安装该模块,由于某种原因,它使用 Clang 而不是 GCC。事实上,如果我输入 `python3',我会得到:

Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

我假设 python3 -m pip install watchdog 会给出一个编译错误,因为 Python 3 没有使用 GCC。错误消息很长,可在此处找到:https://pastebin.com/DEAKANQ9

在我的 $PATH 中,我在 /usr/bin 之前有 /usr/local/bin(其中安装了 gcc),即

echo $PATH
/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

我会说一切都设置正确,但显然情况并非如此。如何让 Python 3 使用 GCC 而不是 Clang?

最佳答案

我已经解决了这个问题。 首先,我删除了使用 pkg 安装程序安装的 Python 3.8(实际上是偶然)。然后,我在 .bash_profile 中为 Python 3 创建了一个别名。另一个错误是我创建了文件 ~/.bashrc(与在 Ubuntu 中一样)而不是编辑文件 ~/.bash_profile 来修改 $PATH。

# ~/.bash_profile
#
# Python alias
alias python=/usr/local/bin/python3
# Setting PATH for Python 3.7.5 (Homebrew)
PATH="/usr/local/bin:${PATH}"
export PATH

# Set module path
PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages

# Set PATH priority to Homebrew installation folder
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

通过此设置,我能够安装模块并使其在我的脚本中运行!

关于MacOS 上的 Python3、gcc 和 clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58890531/

相关文章:

python - 使用python在PostgreSQL中选择变量

python - 获取 html 属性值,属性名称包含冒号和 xpath 表达式

python - gdb 运行时错误 : pretty-printer already registered: libstdc++-v6

python - 格式化字符串 (%s) 返回 TypeError : a float is required

python - 我无法获得要注册为字符串的条目。一直说是float

python - 使用 Telegram Bot 按计划发送消息

python - Tkinter 按钮背景颜色在 mac os 中不起作用

linux - sh 命令没有调用 shell 脚本

python - 让 celery 回归 future

macos - Mac OS X HFS+ 中最长的路径名字符串