我的系统上有太多不同的 Python 版本并导致问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14117945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:32:07  来源:igfitidea点击:

Too many different Python versions on my system and causing problems

pythonmacospathinstallationuninstall

提问by Skyler

During the past years, I have installed many Python libraries with various Python versions. To make them ready to work immediately, I installed them blindly without control. Currently they're causing problems when I tried to install pynestwhich invokes numpy, scipy and matplotlib. After struggling, I am going to clean and reinstall Python and the libraries.

在过去的几年里,我安装了许多不同 Python 版本的 Python 库。为了让它们立即准备好工作,我在没有控制的情况下盲目地安装了它们。目前,当我尝试安装调用 numpy、scipy 和 matplotlib 的pynest时,它们会引起问题。在挣扎之后,我将清理并重新安装 Python 和库。

After investigation, I found Python 2.5/2.6/2.7/3.2 on my system, and each of them has some copies or other things at: (my OS == Mac OS X 10.7.5 Lion)

经过调查,我在我的系统上发现了 Python 2.5/2.6/2.7/3.2,并且每个人都有一些副本或其他东西:(我的操作系统 == Mac OS X 10.7.5 Lion

  • /Library/Frameworks/
  • /opt/local/Library/Frameworks/
  • /opt/local/bin/
  • /Applications/
  • /usr/local/bin/
  • /usr/bin/
  • /System/Library/Frameworks/
  • /Library/Frameworks/
  • /opt/local/Library/Frameworks/
  • /opt/local/bin/
  • /Applications/
  • /usr/local/bin/
  • /usr/bin/
  • /System/Library/Frameworks/

I know I'm crazy to have these. Now I have removed all these except the things in /System/Libarary/Frameworks(I never remove any thing from /System/Library/). After the clean work, which pythonnow gives /usr/bin/pythonwhich links to /System/Library/Frameworks.

我知道我疯了拥有这些。现在我已经删除了所有这些,除了里面的东西/System/Libarary/Frameworks(我从来没有从 中删除任何东西/System/Library/)。在清理工作之后,which python现在给出/usr/bin/python指向/System/Library/Frameworks.

Now, is it a clear environment for me to reinstall python? How to double check that there's no other versions existing? How should I reinstall them to guarantee that they and their libraries won't be everywhere and have many copies again?

现在,我重新安装python的环境是否清晰?如何仔细检查是否存在其他版本?我应该如何重新安装它们以保证它们和它们的库不会无处不在并再次拥有许多副本?

I want to install a clean Python 2.7 onto a proper location, and make my system know exactly where it is and never install any libraries somewhere else. Please give me some advice that how to manage it like in a professional way.

我想将干净的 Python 2.7 安装到适当的位置,并使我的系统确切知道它在哪里,并且永远不要在其他地方安装任何库。请给我一些建议,告诉我如何以专业的方式管理它。

For your information, here is my current $PATH, I think it should be modified:

供您参考,这是我当前的$PATH,我认为应该对其进行修改:

/opt/local/bin:/opt/local/sbin:/opt/nest/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/texbin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/bin:/usr/X11/bin:/opt/local/bin:/opt/local/sbin:/usr/local/lib/python2.7/site-packages:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin

Please let me know If you need more information. Thank you!

如果您需要更多信息,请告诉我。谢谢!



UPDATE:

更新

I'm rethinking profoudly why it becomes so crazy. I believe it's because I installed things via:

我正在重新思考为什么它变得如此疯狂。我相信这是因为我通过以下方式安装了东西:

  • easy_install/ macports/ homebrew/ fink/ pipsometimes;
  • .dmgsometimes;
  • .pkgsometimes;
  • compile source code sometimes;
  • easy_install/ macports/ homebrew/ fink/pip有时;
  • .dmg有时;
  • .pkg有时;
  • 有时编译源代码;

and they made things at different locations. I wonder what's the mechanism behind these ways? How do they choose target location? How to prevent them from messing things up?

他们在不同的地方制作东西。我想知道这些方式背后的机制是什么?他们如何选择目标位置?如何防止他们把事情搞砸?

采纳答案by Skyler

Why did it get messed up?

为什么搞砸了?

There're a couples of different way to install Python, as the update of OP says, and they locate files in different locations. For example, macportsputs things into /opt/local/, while homebrewputs things into /usr/local/. Also, Mac OS X brings a few python versions with itself. So, if you install python many times via different ways, you will get many python versions existing independentlyon your system.

正如OP的更新所说,安装Python有几种不同的方式,它们将文件定位在不同的位置。例如,macports将东西放入/opt/local/,而homebrew将东西放入/usr/local/。此外,Mac OS X 自带一些 python 版本。因此,如果您通过不同的方式多次安装 python,您将在您的系统上独立存在许多 python 版本

What problem does it cause?

它会导致什么问题?

I don't know exactly. I guess the problem is that if you have many versions of python, then which one to use and where to find packages will be determined by the path order in your system PATHand the PYTHONPATHrespectively. So you may lose control of where to install python modules. Consider that if you run sudo python setup.py installto install a module (it finds python by the root's PATH) and then try to importthe module by python -c "import it"(this time it finds python by your PATH), maybe something will go wrong. This is my guess, I didn't validate it. But in my own case, something did go wrong.

我不知道确切。我想问题是如果你有很多版本的 python,那么使用哪个版本和在哪里找到包将取决于你系统中的路径顺序PATHPYTHONPATH分别。因此,您可能无法控制安装 python 模块的位置。考虑一下,如果您运行sudo python setup.py install以安装模块(它通过根的 找到 python PATH)然后尝试import通过python -c "import it"(这次它通过您的 找到 python PATH)模块,可能会出错。这是我的猜测,我没有验证它。但就我自己而言,确实出了点问题。

How to avoid this?

如何避免这种情况?

I think the principle would be that be aware of that different ways and tools install things independently to different locations, so use them mindfully.

我认为原则是要意识到不同的方式和工具将东西独立地安装到不同的位置,因此请谨慎使用它们

  • Unless you intend to, don't install the same thing twice via different ways. (If you intend to do it for python, you might want to check out virtualenv)
  • Keep an eye on the path order in your PATHand consider if it's correct.
  • When installing modules, be clear which python (or pip) is running and where the module is installed.
  • 除非您打算,否则不要通过不同的方式安装相同的东西两次。(如果你打算为 python 做这件事,你可能想看看virtualenv
  • PATH注意你的路径顺序并考虑它是否正确。
  • 安装模块时,要清楚哪个python(或pip)正在运行以及模块安装在哪里。

So, how did I solve my own case?

那么,我是如何解决自己的案子的?

Since it had been messing up already and seemed to be very hard to cure, so finally I solved this question by a full OS re-installation, and started to follow the DOs-and-DONTsabove. For the installation of the scientific environment with python (numpy/scipy/matplotlib, which had shown problems to make me ask this question), I found this tutorialwas extremely helpful. So, problem solved finally.

由于它已经搞砸了并且似乎很难治愈,所以最后我通过重新安装完整的操作系统解决了这个问题,并开始遵循上面的DOs-and-DONTs。对于用python安装科学环境(numpy/scipy/matplotlib,曾经出现问题让我问这个问题),我发现这个教程非常有帮助。所以,问题终于解决了。

回答by gecco

In order to install a python distributions into specific folder, you can use the --prefixscheme during python installation. Using the prefix scheme, you can for example install Python 2.7 into the folder /opt/py27. Now, in order to use the new installed Python distribution you have to: cleanupyou PATHand LD_LIBRARY_PATH:

为了将 python 发行版安装到特定文件夹中,您可以--prefix在 python 安装过程中使用该方案。例如,使用前缀方案,您可以将 Python 2.7 安装到文件夹中/opt/py27。现在,为了使用新安装的Python发行,你必须:清理PATHLD_LIBRARY_PATH

  • Remove all 'old' Python paths and
  • configure (according to my example) the environment variables like this:
    • PATH: Add /opt/py27/bin
    • LD_LIBRARY_PATH: Add /opt/py27/lib
  • 删除所有“旧”Python 路径和
  • 配置(根据我的例子)环境变量是这样的:
    • 路径:添加 /opt/py27/bin
    • LD_LIBRARY_PATH:添加/opt/py27/lib

That's it.

就是这样。

(In case you need multiple environments of Python installed at the same time, I'd suggest to have a look at virtualenv)

(如果您需要同时安装多个 Python 环境,我建议您查看virtualenv

回答by jersey bean

Here is what was confusing me and how I solved it.

这是让我感到困惑的原因以及我如何解决它。

$ which python
/usr/bin/python 

$ which python3
/usr/local/bin/python3

$ ls /usr/local/bin/python
ls: /usr/local/bin/python: No such file or directory

So notice I didn't have a HomeBrew installation of python2.7, but did have the python3 installation. The version under /usr/bin/python is using the system default. You can tell based on the module search path:

所以请注意,我没有安装 python2.7 的 HomeBrew,但安装了 python3。/usr/bin/python 下的版本是使用系统默认的。您可以根据模块搜索路径判断:

$ /usr/bin/python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
`enter code here`Type "help", "copyright", "credits" or "license" for 
more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/...

Notice the '/Library/Python'... that's Mac OS's version of python. But I want to stay strictly on a user installed version (i.e. HomeBrew).

注意'/Library/Python'...这是Mac OS 的python 版本。但我想严格遵守用户安装的版本(即 HomeBrew)。

So here's what I did to fix this:

所以这是我为解决这个问题所做的:

$ brew install python
...
Warning: python 2.7.13 is already installed, it's just not linked.
You can use `brew link python` to link this version.

$ brew link --overwrite python

$ which python
/usr/local/bin/python

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/Cellar/python/2.7.13...

Its no longer /Library/.. but /usr/local.

它不再是 /Library/.. 而是 /usr/local。

Now its finding all of my pip installed modules! Problem solved!

现在它找到了我所有的 pip 安装模块!问题解决了!



UPDATE:

更新:

After updating brew to version 1.5.4, it seems the symbolic links were removed. And now you have to add this to your path:

将 brew 更新到 1.5.4 版后,符号链接似乎已被删除。现在您必须将其添加到您的路径中:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Read the Caveats section in 'brew info python':

阅读“brew info python”中的注意事项部分:

==> Caveats
This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
  export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Pip and setuptools have been installed. To update them
  pip2 install --upgrade pip setuptools

You can install Python packages with
  pip2 install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python.html

回答by jersey bean

Here's another great solution to managing different python versions:

这是管理不同 python 版本的另一个很好的解决方案:

https://github.com/pyenv/pyenv

https://github.com/pyenv/pyenv

(I already provided an answer here, but decided to post this as an additional or alternative answer)

(我已经在这里提供了答案,但决定将此作为附加或替代答案发布)