【问题标题】:NPM install is not allowing me to specify the Python versionNPM 安装不允许我指定 Python 版本
【发布时间】:2021-01-27 04:52:42
【问题描述】:

我在尝试使用 npm install -g <package-name> 安装的自定义 NPM 包遇到新问题。这个包有一个“安装后”步骤,运行pip install。要求适用于 Python 3 包,但 pip 安装失败,因为它在 Python 2 中运行:

> <package-name> postinstall /usr/local/lib/node_modules/<package-name>
> pip install --upgrade -r ./scripts/requirements.txt

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. [...removing for clarity...]
Defaulting to user installation because normal site-packages is not writeable
[...]
npm ERR! Failed at the <package-name>@<version> postinstall script.

我尝试在 Python 3 venv 环境中运行全局安装,并尝试使用 npm config set python python3.8 将 npm 指向 Python 3,基于 this question。似乎没有什么可以强制 postinstall 使用 Python 3 版本的 pip 运行,甚至没有直接指定 python 路径。

仅供参考:我的initial question 已关闭,因为它与上面链接的相似,但正如我所说,这对我没有帮助。另外,我在 macOS Mojave 上。

另外,当我在本地运行 npm run postinstall 时 - 在包的目录中 - 我得到了同样的错误。如果我只运行pip install 行,它就可以正常工作。在这里使用 npm run 会改变行为,但我不确定它是什么。

这是我公司维护的内部包。 pip install 行是唯一的安装后步骤。它安装了一系列要求,包括 matplotlib >= 3.0(因此需要 Python 3 - 这通常是它失败的地方)。我将pip install 更改为python -m pip install,实际上确实使用了正确的python 版本,然后在matplotlib 设置中失败。我认为这是关键。

【问题讨论】:

  • 什么包?
  • 该包是我公司维护的内部包
  • 那么我们无能为力,除非您可以分享正在使用 pip 的位的minimal reproducible example
  • 真的就是上面那一行,pip install --upgrade -r ./scripts/requirements.txt。要求包括 matplotlib>=3.0,这是它失败的地方。我在帖子中添加了一些细节

标签: python macos npm installation pip


【解决方案1】:

你试过这个命令:

  echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc
  echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

还是使用 brew?

  brew update && brew upgrade python

如果您没有,请将此别名放在您的 .rc 文件中:

  alias python=/usr/local/bin/python3

看到这个页面他们有很多技巧:https://opensource.com/article/19/5/python-3-default-mac

【讨论】:

  • 我尝试向 ~/.bashrc 添加别名,但没有成功。我使用 brew,昨天我运行了brew update &amp;&amp; brew upgrade python。该包是我公司维护的内部包
  • 您尝试将命令更改为 pip3 -> pip3 install --upgrade -r ./scripts/requirements.txt?
  • 不,我不想这样做,因为它不会被所有机器支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2018-05-26
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多