Python版本混乱,无法成功安装软件包。

huangapple go评论75阅读模式
英文:

Python versions mess not able to successfully install packages anymore

问题

我正在使用Python版本3.11,搭载MacOS Ventura 13.1(M1芯片),但无法成功安装包。在安装它们后(显然成功),Python找不到它们,因此也无法卸载它们。

我已经安装了很多与研究相关的重要包,它们位于/opt/homebrew/lib/python3.10/site-packages/文件夹下。如何让它们在Python 3.11下工作呢?

还有一些问题:
a) 由于某种原因,pip install不再起作用,但pip3 install可以,这正常吗?
b) 每当我安装东西时,它会显示:

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: python3.11 -m pip install --upgrade pip
user@MacBook-user ~ % python3.11 -m pip install --upgrade pip
Collecting pip
  Using cached pip-23.0-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
Successfully installed pip-23.0

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: /opt/homebrew/opt/python@3.11/bin/python3.11 -m pip install --upgrade pip

所以它似乎会重复显示消息,尽管它说已成功安装了pip。

我已经尝试卸载和重新安装Python,但似乎也不起作用。
(我偶尔在安装特定包时似乎也有问题,所以我认为有些东西不对,但重新安装和升级都没有奏效)

根据您的评论:

which python3.11显示/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 - 我该如何更改设置,以便与之前的包一起使用?
opt/homebrew/opt/python3.11/opt/homebrew/Cellar/python@3.11/3.11.1 的别名。


更新:
因为整个M1的问题似乎一团糟,我已经卸载并重新安装了brew、CommandLine Tools和Python。

我现在正在运行Python 3.10.9,即使是新安装的,它仍然会显示:

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: python3.10 -m pip install --upgrade pip

现在是:

which python3.10
/opt/homebrew/bin/python3.10

(无论我是否像上面那样更新,都会一直出现上面的提示)。

英文:

I am using Python version 3.11 with MacOS Ventura 13.1 (M1 chip) and cannot successfully install packages anymote. Upon installing them (apparently successfully) Python does not find them and is thus also not able to uninstall them.

I have a lot of research related and thus really important packages installed under a folder opt/homebrew/lib/python3.10/site-packages/

How can I get them to work with 3.11?
And a few more questions:
a) For some reason pip install doesn't work anymore, pip3 install does however, is that normal?
b) whenever I install something it says:

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: python3.11 -m pip install --upgrade pip
user@MacBook-user ~ % python3.11 -m pip install --upgrade pip
Collecting pip
  Using cached pip-23.0-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
Successfully installed pip-23.0

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: /opt/homebrew/opt/python@3.11/bin/python3.11 -m pip install --upgrade pip

so it kind of shows the message on repeat although it says, it has successfully installed pip.

I have already tried to uninstall and reinstall python, but it does not seem to work either.
(I also seem to have some issue with installing specific packages from time to time, which is why I assume something is off, but neither reinstalling nor upgrading has worked)

As per the comment:

which python3.11 shows /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 - how can I change my setup so it will work with my previous packages?
opt/homebrew/opt/python3.11 is an alias for /opt/homebrew/Cellar/python@3.11/3.11.1


Update:
since the whole M1 thing seems to be a mess, I un- and reinstalled brew, CommandLine Tools and python

I am running things on python 3.10.9 now, having it freshly installed it still says
[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: python3.10 -m pip install --upgrade pip

it now is:
which python3.10
/opt/homebrew/bin/python3.10

(regardless of whether I update like the above python3.10 -m pip install --upgrade pip or pip3 -m pip install --upgrade pip or python3 -m pip install --upgrade pip it will always keep complaining with the note above.

答案1

得分: 1

当你在shell中输入python时,运行的Python解释器取决于你的PATH环境变量 - 除非你使用alias命令,关于为什么建议不使用alias请参考底部的注释。

路径中包含“local”或“homebrew”一词通常是由homebrew安装的项目。

/Library开头或包含Frameworks的路径通常是由Apple提供的项目,不应删除。

你似乎正在运行由Apple提供的Python,所以你的PATH设置是错误的。如果你想运行homebrew的Python,你需要运行:

brew info Python

并按照其中关于版本化链接、非版本化链接和PATH的说明进行操作。


请注意,Python和pip的版本是紧密关联的。你必须运行与你的Python版本匹配的pip,否则你将陷入更新错误版本的死循环。


你可以通过运行以下命令来查看在输入python时将运行哪个Python解释器:

type python

同样,你可以通过运行以下命令来查看将启动哪个pip

type pip

你可以从脚本内部或IDE内部查看正在运行的Python以及它查找包的位置:

import sys
# 正在运行的Python的路径
print(sys.executable)
# 它查找包的位置
print(sys.path)

# 它是如何找到Numpy或其他包的位置的?
import numpy as np
print(np.__file__)

你可以使用以下命令查看pip安装的包的位置,比如flask

pip show flask

注意:你可以像这样定义一个“别名”:

alias python=/some/path/to/some/python

在我看来,这是一个不好的主意,因为别名在以下情况下不受尊重:

  • 定时任务(cron jobs),所以当你尝试按计划运行Python进程时,会出现问题。
  • Python子进程,所以当你调用subprocess.run(...)时,会出现问题。
  • IDEs,所以当你使用PyCharm、VS Code等工具时,会出现问题。

此外,如果你将python的别名设置为某个版本的Python 3.10,但不将pip的别名设置为相同的版本,你将陷入混乱之中。

注意homebrew将其软件包安装在其“Cellar”中,然后从/usr/local/bin/opt/homebrew/bin创建符号链接(symlinks)。一般来说,你应该使用后者而不是直接使用Cellar中的二进制文件,因为这样可以实现以下效果:

  • homebrew可以管理链接,以便你可以轻松切换版本。
  • 你只需一个PATH即可使用所有homebrew软件包。
英文:

The Python interpreter that runs when you type python in your shell is determined by your PATH - unless you use the alias command, see note at bottom about why I suggest you do not use that.

Paths with the word "local" or "homebrew" in them are generally items installed by homebrew.

Paths starting with /Library or containing Frameworks are generally items supplied by Apple and should not be removed.

You appear to be running an Apple-supplied Python, so your PATH is wrong. If you want to run the homebrew Python, you need to run:

brew info Python

and read and do exactly what it says in respect of versioned links, unversioned links and your PATH.


Note that Python and pip versions are inextricably linked. You must run the pip that matches your Python or you will go around in circles updating the wrong one.


You can see which Python you'll run when entering python in your shell by running:

type python

Likewise, you can see which pip you'll start by running:

type pip

You can see which Python you are running from inside a script or inside an IDE, and also where it is looking for packages:

import sys
# Where is the Python I am running?
print(sys.executable)
# Where is it looking for packages?
print(sys.path)

# Where did it find Numpy, or any other package?
import numpy as np
print(np.__file__)

You can see where pip installed a package, say flask with:

pip show flask

Note: It is possible to define an "alias" like this:

alias python=/some/path/to/some/python

IMHO, that is a bad idea as aliases are not respected in:

  • cron jobs, so you'll get problems as soon as you try to run a Python process on a schedule
  • Python subprocesses, so you'll get problems as soon as you call suborocess.run(...)
  • IDEs, so you'll get problems when you use PyCharm, VS Code and so on.

Also, if you alias python to some version 3.10 but don't alias pip to the same version, you will get in a mess.

Note: homebrew installs its packages in its "Cellar" and then makes symbolic links (symlinks) to them from /usr/local/bin or /opt/homebrew/bin. In general, you should use the latter rather than directly using the binaries in the Cellar because then:

  • homebrew can manage the links to allow you to switch versions simply
  • you'll only need one PATH to allow you to use all homebrew packages

huangapple
  • 本文由 发表于 2023年2月6日 12:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定