标签归档:homebrew

如何使用自制软件在macOS中安装Python 3的早期版本?

问题:如何使用自制软件在macOS中安装Python 3的早期版本?

如何使用brew在macOS中安装Python 3的早期版本?

通过该命令,brew install python我获得了最新版本的Python 3(当前为v3.7.0),但我想要的是最新版本的Python 3.6(当前为3.6.5)。

我已经阅读了另一个pyenv可以帮助处理不同python安装的软件包,但是此解决方案不适合我。

How can I install a previous version of Python 3 in macOS using brew?

With the command brew install python I got the latest version of Python 3 (currently v3.7.0), but I want the last version of Python 3.6 (currently 3.6.5).

I have read about another package pyenv that can assist in handle different python installation, but this solution is not suitable for me.


回答 0

简短答案

要进行Python 3.6.5的全新安装,请使用:

brew unlink python # ONLY if you have installed (with brew) another version of python 3
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

如果您希望恢复以前安装的版本,则:

brew info python           # To see what you have previously installed
brew switch python 3.x.x_x # Ex. 3.6.5_1

长答案

使用Homebrew安装Python的公式有两个:python@2python
第一个用于Python 2,第二个用于Python 3。

注意:您可以在网上找到过时的答案,这些答案被python3称为安装Python版本3的公式名称python

默认情况下,使用这些公式,您可以安装对应的Python主版本的最新版本。因此,您不能直接安装3.6的次要版本。

使用brew,您可以使用公式的地址安装软件包,例如在git存储库中。

brew install https://the/address/to/the/formula/FORMULA_NAME.rb

或专门针对Python 3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/COMMIT_IDENTIFIER/Formula/python.rb

您必须指定的地址是所需版本的公式的最后提交地址(python.rb)。您可以通过查看homebrew-core / Formula / python.rb的历史记录来找到commint标识符

https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb

Python> 3.6.5

在上面的链接中,您不会找到3.6.5以上版本的Python的公式。该(官方)存储库的维护者发布Python 3.7之后,他们仅提交对Python 3.7配方的更新。

如上所述,使用自制软件时,您只有Python 2(python @ 2)和Python 3(python),而Python 3.6没有明确的公式

尽管那些次要更新在大多数情况下和对于大多数用户而言都是无关紧要的,但我将搜索是否有人对3.6做过明确的公式。

Short Answer

To make a clean install of Python 3.6.5 use:

brew unlink python # ONLY if you have installed (with brew) another version of python 3
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

If you prefer to recover a previously installed version, then:

brew info python           # To see what you have previously installed
brew switch python 3.x.x_x # Ex. 3.6.5_1

Long Answer

There are two formulas for installing Python with Homebrew: python@2 and python.
The first is for Python 2 and the second for Python 3.

Note: You can find outdated answers on the web where it is mentioned python3 as the formula name for installing Python version 3. Now it’s just python!

By default, with these formulas you can install the latest version of the corresponding major version of Python. So, you cannot directly install a minor version like 3.6.

Solution

With brew, you can install a package using the address of the formula, for example in a git repository.

brew install https://the/address/to/the/formula/FORMULA_NAME.rb

Or specifically for Python 3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/COMMIT_IDENTIFIER/Formula/python.rb

The address you must specify is the address to the last commit of the formula (python.rb) for the desired version. You can find the commint identifier by looking at the history for homebrew-core/Formula/python.rb

https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb

Python > 3.6.5

In the link above you will not find a formula for a version of Python above 3.6.5. After the maintainers of that (official) repository released Python 3.7, they only submit updates to the recipe of Python 3.7.

As explained above, with homebrew you have only Python 2 (python@2) and Python 3 (python), there is no explicit formula for Python 3.6.

Although those minor updates are mostly irrelevant in most cases and for most users, I will search if someone has done an explicit formula for 3.6.


回答 1

作为更新,当做

brew unlink python # If you have installed (with brew) another version of python
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

你可能会遇到

Error: python contains a recursive dependency on itself:
  python depends on sphinx-doc
  sphinx-doc depends on python

要绕过它,请将--ignore-dependencies参数添加到brew install中。

brew unlink python # If you have installed (with brew) another version of python
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

As an update, when doing

brew unlink python # If you have installed (with brew) another version of python
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

You may encounter

Error: python contains a recursive dependency on itself:
  python depends on sphinx-doc
  sphinx-doc depends on python

To bypass it, add the --ignore-dependencies argument to brew install.

brew unlink python # If you have installed (with brew) another version of python
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

回答 2

我所做的是首先安装python 3.7

brew install python3
brew unlink python

然后我使用上面的链接安装了python 3.6.5

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies

之后我跑了brew link --overwrite python。现在,我在系统中拥有所有的python来创建虚拟环境。

mian@tdowrick2~ $ python --version
Python 2.7.10
mian@tdowrick2~ $ python3.7 --version
Python 3.7.1
mian@tdowrick2~ $ python3.6 --version
Python 3.6.5

创建Python 3.7虚拟环境。

mian@tdowrick2~ $ virtualenv -p python3.7 env
Already using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/mian/env/bin/python3.7
Also creating executable in /Users/mian/env/bin/python
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.7.1
(env) mian@tdowrick2~ $ deactivate

创建Python 3.6虚拟环境

mian@tdowrick2~ $ virtualenv -p python3.6 env
Running virtualenv with interpreter /usr/local/bin/python3.6
Using base prefix '/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/mian/env/bin/python3.6
Not overwriting existing python script /Users/mian/env/bin/python (you must use /Users/mian/env/bin/python3.6)
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.6.5
(env) mian@tdowrick2~ $ 

What I did was first I installed python 3.7

brew install python3
brew unlink python

then I installed python 3.6.5 using above link

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies

After that I ran brew link --overwrite python. Now I have all pythons in the system to create the virtual environments.

mian@tdowrick2~ $ python --version
Python 2.7.10
mian@tdowrick2~ $ python3.7 --version
Python 3.7.1
mian@tdowrick2~ $ python3.6 --version
Python 3.6.5

To create Python 3.7 virtual environment.

mian@tdowrick2~ $ virtualenv -p python3.7 env
Already using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/mian/env/bin/python3.7
Also creating executable in /Users/mian/env/bin/python
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.7.1
(env) mian@tdowrick2~ $ deactivate

To create Python 3.6 virtual environment

mian@tdowrick2~ $ virtualenv -p python3.6 env
Running virtualenv with interpreter /usr/local/bin/python3.6
Using base prefix '/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/mian/env/bin/python3.6
Not overwriting existing python script /Users/mian/env/bin/python (you must use /Users/mian/env/bin/python3.6)
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.6.5
(env) mian@tdowrick2~ $ 

回答 3

我尝试了以上所有答案来安装Python 3.4.4。python的安装有效,但不会安装PIP,并且我无能为力。我使用的是Mac OSX Mojave,这会导致zlib,openssl出现一些问题。

不该做什么:

  • 尝试避免对公式PythonPython3给定的早期版本使用Homebrew
  • 不要尝试编译Python

解:

  1. 下载macOS 64位安装程序macOS 64位/ 32位安装程序https : //www.python.org/downloads/release/python-365/
  2. 在前面的步骤,它会下载的Python 3.6.5,例如如果你想下载的Python 3.4.4,在上面的网址取代Python-365Python-344
  3. 下载单击您下载的文件,GUI安装程序将打开
  4. 如果您下载了python-365,则在安装后要启动此版本的python,您将在终端python365中键入,对pip相同,即为pip365

ps:您不必在系统上卸载其他版本的Python。


编辑:


我发现了一种更好的解决方案,可在MacOSX,Windows,Linux等系统上运行。

  1. 是否已经安装python都没关系。
  2. 下载Anaconda
  3. 安装后,在终端中输入: conda init
  4. 在终端中,使用任何 python版本创建虚拟环境,例如,我选择了3.4.4:conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
  5. 然后,在终端中,您可以使用以下命令检查已创建的所有虚拟环境: conda info --envs
  6. 然后,在终端中,使用以下命令激活您选择的虚拟环境: conda activate [The name of your virtual environment that was shown with the command at step 5]

I tried all the answers above to install Python 3.4.4. The installation of python worked, but PIP would not be installed and nothing I could do to make it work. I was using Mac OSX Mojave, which cause some issues with zlib, openssl.

What not to do:

  • Try to avoid using Homebrew for previous version given by the formula Python or Python3.
  • Do not try to compile Python

Solution:

  1. Download the macOS 64-bit installer or macOS 64-bit/32-bit installer: https://www.python.org/downloads/release/python-365/
  2. In previous step, it will download Python 3.6.5, if for example, you want to download Python 3.4.4, replace in the url above python-365 by python-344
  3. Download click on the file you downloaded a GUI installer will open
  4. If you downloaded python-365, after installation, to launch this version of python, you will type in your terminal python365, same thing for pip, it will be pip365

p.s: You don’t have to uninstall your other version of Python on your system.


Edit:


I found a much much much better solution that work on MacOSX, Windows, Linux, etc.

  1. It doesn’t matter if you have already python installed or not.
  2. Download Anaconda
  3. Once installed, in terminal type: conda init
  4. In terminal,create virtual environment with any python version, for example, I picked 3.4.4: conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
  5. Then, in terminal, you can check all the virtual environment you ahave created with the command: conda info --envs
  6. Then, in terminal, activate the virtual environment of your choice with: conda activate [The name of your virtual environment that was shown with the command at step 5]

回答 4

我已经尝试了所有方法,但无法使其正常工作。最终,我使用了pyenv它,并且像魅力一样直接工作。

因此,homebrew安装完毕后,只需执行以下操作:

brew install pyenv
pyenv install 3.6.5

管理virtualenvs:

brew install pyenv-virtualenv
pyenv virtualenv 3.6.5 env_name

有关更多信息,请参见pyenvpyenv-virtualenv

编辑(2019/03/19)

我发现使用pyenv-installer比自制软件更容易安装pyenv和pyenv-virtualenv direclty:

curl https://pyenv.run | bash

要全局管理python版本:

pyenv global 3.6.5

或本地给定目录中:

pyenv local 3.6.5

I have tried everything but could not make it work. Finally I have used pyenv and it worked directly like a charm.

So having homebrew installed, juste do:

brew install pyenv
pyenv install 3.6.5

to manage virtualenvs:

brew install pyenv-virtualenv
pyenv virtualenv 3.6.5 env_name

See pyenv and pyenv-virtualenv for more info.

EDIT (2019/03/19)

I have found using the pyenv-installer easier than homebrew to install pyenv and pyenv-virtualenv direclty:

curl https://pyenv.run | bash

To manage python version, either globally:

pyenv global 3.6.5

or locally in a given directory:

pyenv local 3.6.5

回答 5

万一有人遇到如下点子问题

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

根本原因是openssl 1.1不再支持python 3.6。所以你需要安装旧版本的openssl 1.0

这是解决方案:

brew uninstall --ignore-dependencies openssl
brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

In case anyone face pip issue like below

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

The root cause is openssl 1.1 doesn’t support python 3.6 anymore. So you need to install old version openssl 1.0

here is the solution:

brew uninstall --ignore-dependencies openssl
brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

回答 6

要解决此问题homebrew,您可以临时回溯日期homebrew-core并设置HOMEBREW_NO_AUTO_UPDATE变量以将其保留在适当的位置:

cd `brew --repo homebrew/core`
git checkout f2a764ef944b1080be64bd88dca9a1d80130c558
export HOMEBREW_NO_AUTO_UPDATE=1
brew install python

我不建议永久回溯自制内核的日期,因为您会错过安全补丁,但是它对于测试很有用。

您还可以使用以下brew extract命令将旧版本的自制程序公式提取到自己的水龙头(tap_owner / tap_name)中:

brew extract python tap_owner/tap_name --version=3.6.5

To solve this with homebrew, you can temporarily backdate homebrew-core and set the HOMEBREW_NO_AUTO_UPDATE variable to hold it in place:

cd `brew --repo homebrew/core`
git checkout f2a764ef944b1080be64bd88dca9a1d80130c558
export HOMEBREW_NO_AUTO_UPDATE=1
brew install python

I don’t recommend permanently backdating homebrew-core, as you will miss out on security patches, but it is useful for testing purposes.

You can also extract old versions of homebrew formulae into your own tap (tap_owner/tap_name) using the brew extract command:

brew extract python tap_owner/tap_name --version=3.6.5

回答 7

对我而言,最简单的方法是安装Anaconda:https : //docs.anaconda.com/anaconda/install/

在这里,我可以根据需要创建任意数量的具有不同Python版本的环境,然后单击鼠标即可在它们之间切换。再简单不过了。

要安装不同的Python版本,只需按照以下说明进行操作即可:https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html

在2分钟内完成了具有其他Python版本的新开发环境。将来我可以轻松地来回切换。

The easiest way for me was to install Anaconda: https://docs.anaconda.com/anaconda/install/

There I can create as many environments with different Python versions as I want and switch between them with a mouse click. It could not be easier.

To install different Python versions just follow these instructions https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html

A new development environment with a different Python version was done within 2 minutes. And in the future I can easily switch back and forth.


使用任何hg mercurial命令时,“找不到哈希md5的ERROR:root:code”

问题:使用任何hg mercurial命令时,“找不到哈希md5的ERROR:root:code”

尝试hg在控制台上使用任何Mercurial命令时,我一直收到此错误。我使用Homebrew安装了Python,并且正在运行Mac OS Catalina v.1.5.1。

任何参考将不胜感激。这是我得到的错误:

hg commit --amend
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/usr/local/bin/hg", line 43, in <module>
    dispatch.run()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 625, in <module>
    class lazyaliasentry(object):
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 636, in lazyaliasentry
    @util.propertycache
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/util.py", line 180, in <module>
    'md5': hashlib.md5,
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 151, in __getattr__
    return getattr(self._module, attr)
AttributeError: 'module' object has no attribute 'md5'

我也尝试按照此问题上的说明进行操作,但所有解决方案似乎均不起作用

brew link openssl --force
Warning: Refusing to link macOS-provided software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

When trying to use any hg Mercurial commands on the console, I keep getting this error. I installed Python using Homebrew and I am running Mac OS Catalina v. 10.15.1.

Any reference would be appreciated. Here is the error I’m getting:

hg commit --amend
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/usr/local/bin/hg", line 43, in <module>
    dispatch.run()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 625, in <module>
    class lazyaliasentry(object):
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 636, in lazyaliasentry
    @util.propertycache
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/util.py", line 180, in <module>
    'md5': hashlib.md5,
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 151, in __getattr__
    return getattr(self._module, attr)
AttributeError: 'module' object has no attribute 'md5'

I also tried following the instruction on this issue but none of the solutions seem to work

brew link openssl --force
Warning: Refusing to link macOS-provided software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

回答 0

brew reinstall python@2对于我现有的Python 2.7虚拟环境,运行不起作用。在他们里面仍然有ERROR:root:code for hash sha1 was not found错误。

我跑步后遇到了这个问题brew upgrade openssl。解决方法是:

$ ls /usr/local/Cellar/openssl

…这表明

1.0.2t

根据现有版本,运行:

$ brew switch openssl 1.0.2t

…这表明

Cleaning /usr/local/Cellar/openssl/1.0.2t
Opt link created for /usr/local/Cellar/openssl/1.0.2t

之后,在Python 2.7 virtualenv中运行以下命令:

(my-venv) $ python -c "import hashlib;m=hashlib.md5();print(m.hexdigest())"

…这表明

d41d8cd98f00b204e9800998ecf8427e

没有更多的错误。

Running brew reinstall python@2 didn’t work for my existing Python 2.7 virtual environments. Inside them there were still ERROR:root:code for hash sha1 was not found errors.

I encountered this problem after I ran brew upgrade openssl. And here’s the fix:

$ ls /usr/local/Cellar/openssl

…which shows

1.0.2t

According to the existing version, run:

$ brew switch openssl 1.0.2t

…which shows

Cleaning /usr/local/Cellar/openssl/1.0.2t
Opt link created for /usr/local/Cellar/openssl/1.0.2t

After that, run the following command in a Python 2.7 virtualenv:

(my-venv) $ python -c "import hashlib;m=hashlib.md5();print(m.hexdigest())"

…which shows

d41d8cd98f00b204e9800998ecf8427e

No more errors.


回答 1

通过首先取消链接openssl来解决此问题

brew unlink openssl

然后重新安装python

brew reinstall python@2

我还注意到,在运行“ brew doctor”时,在/ usr / local / include / node /中有一个与openssl文件夹有关的警告。我在运行上述命令之前删除了此文件夹(不确定是否相关)

Managed to fix this by first unlinking openssl

brew unlink openssl

And then reinstalling python

brew reinstall python@2

I also noticed that when running ‘brew doctor’ there was a warning related to an openssl folder found in /usr/local/include/node/. I deleted this folder before running the above commands (not sure if related)


回答 2

对我来说,当我安装django网络应用程序的依赖项时,会弄乱环境。当我打字cd,它显示相同的错误。

问题是openssl图书馆,找不到正确的图书馆。

如果您使用的是Macintosh,则可以输入

ls /usr/local/Cellar/openssl

查看所有版本,

brew switch openssl 1.0.XXXX

选择可用的openssl版本。

然后错误消失了:)

The case for me is that when I install dependencies of a django web app, it messes up the environment. When I type cd, it shows the same error.

The problem was the openssl library, it can not find the correct ones.

If you are on Macintosh, you can type

ls /usr/local/Cellar/openssl

to see all the versions,

brew switch openssl 1.0.XXXX

to choose the available openssl version.

Then the error is gone :)


回答 3

当我导入hashlib时,会看到一条错误消息,指出未找到哈希md5。

我能够通过首先取消链接openssl来解决此问题:brew unlink openssl

然后,我使用MacPorts卸载了python 2.7:sudo port卸载python27

然后我使用MacPorts安装了python 2.7:sudo port install python27

现在导入hashlib可以工作了:)

When I would import hashlib I would see an error message stating that hash md5 was not found.

I was able to fix this problem by first unlinking openssl: brew unlink openssl

Then I uninstalled python 2.7 using MacPorts: sudo port uninstall python27

Then I installed python 2.7 using MacPorts: sudo port install python27

Now importing hashlib works :)


回答 4

只需卸载python2

$ brew uninstall python@2

如果有任何错误:

$ brew uninstall --ignore-dependencies python@2

Just uninstall python2

$ brew uninstall python@2

If there is any error :

$ brew uninstall --ignore-dependencies python@2


如何使用Homebrew在Mac上安装Python 2和3?

问题:如何使用Homebrew在Mac上安装Python 2和3?

我需要能够在Python 2和3之间来回切换。如何使用Homebrew做到这一点,因为我不想弄乱路径并遇到麻烦。现在,我通过Homebrew安装了2.7。

I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don’t want to mess with path and get into trouble. Right now I have 2.7 installed through Homebrew.


回答 0

我会用pyenv您可以安装它:

$ brew install pyenv

要在您的Bash shell中启用pyenv,您需要运行:

$ eval "$(pyenv init -)"

要在启动时自动为Bash执行此操作,请将该行添加到您的中~/.bash_profile1个

用法:

安装并激活pyenv后,您可以安装不同版本的python并选择可以使用的版本。例:

$ pyenv install 2.7.5

您可以检查已安装的版本:

$ pyenv versions

您可以使用以下命令在python版本之间进行切换:

$ pyenv global 3.3.1

您还可以使用以下命令为当前目录设置python版本:

$ pyenv local 3.5.2

您可以通过运行python --version以下命令进行检查:

$ python --version
Python 3.5.2

1 Homebrew曾经指示您在安装pyenv时执行此操作,但是该消息已删除。对于Zsh和其他Shell,精确步骤可能有所不同。

I would use pyenv You can install it:

$ brew install pyenv

To enable pyenv in your Bash shell, you need to run:

$ eval "$(pyenv init -)"

To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1

Usage:

Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:

$ pyenv install 2.7.5

You can check the versions you have installed with:

$ pyenv versions

And you can switch between python versions with the command:

$ pyenv global 3.3.1

Also you can set a python version for the current directory with:

$ pyenv local 3.5.2

You can check by running python --version:

$ python --version
Python 3.5.2

1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.


回答 1

您可以同时安装两个版本。

对于Homebrew> = 1.5.0:

自2018年3月1日起,该python公式将升级到Python 3.x,同时python@2专门为Python 2.7添加一个新公式。

在此处查看更改公告,或在此处查看有关将Homebrew用于Python最终文档

对于较早的自制软件:

对于Python 2.x:

brew install python

对于Python 3.x:

brew install python3

现在,您将在计算机中同时安装两个版本。当您要使用版本2时,请使用python可执行文件。当您要使用版本3时,请使用python3可执行文件。

You can have both versions installed at the same time.

For Homebrew >=1.5.0:

Since 1st March 2018 the python formula will be upgraded to Python 3.x, while a new python@2 formula will be added for Python 2.7, specifically.

See changes announcement here or the final doc about using Homebrew for Python here.

For older Homebrew:

For Python 2.x:

brew install python

For Python 3.x:

brew install python3

Now, you will have both the versions installed in your machine. When you want to use version 2, use the python executable. When you want to use version 3, use the python3 executable.


回答 2

当前,Homebrew为Python 2和3提供了两个不同的公式。brew install python安装python3,然后brew install python@2安装python2。Homebrew文档中的更多详细信息:

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

如果您当前通过Homebrew安装了2.x,Homebrew会给您以下消息:

Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`

如果您运行:

brew upgrade python

您应该能够:

python --version

python3 --version

查看安装了什么版本的Python 2.x和3.x。

Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python@2 installs python2. More details in Homebrew docs:

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

If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:

Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`

If you run:

brew upgrade python

you should be able to do:

python --version

and

python3 --version

To see what versions of Python 2.x and 3.x installed.


回答 3

另外,您可能只需输入“ python3”来运行最新版本的python3.x,然后输入“ python”或“ python2”来运行最新安装的2.x版本。

Alternatively, you probably can just enter “python3” to run your most current version of python3.x and “python” or “python2” to run the latest installed 2.x version.


回答 4

有两种方法都可以使用,但是今天最简单的解决方案是使用pyenv。pyenv允许在版本之间轻松切换。这是我要做的设置:

第1步:

从Mac移除所有python

 brew uninstall --ignore-dependencies --force python
 sudo rm -rf ~/miniconda3/
 sudo rm -rf ~/.conda/

从以下内容删除 ~/.bash_profile

export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"

还有以下内容 ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"

第2步

安装pyenv和所需的python版本

brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0

第三步

添加pyenv initbash_profilebashrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

第4步

检查安装了什么

pyenv versions
  • 系统(由/Users/ishandutta2007/.pyenv/version设置)

    2.7

    3.7.0

第5步:

选择一个默认值

pyenv global 3.7.0

当项目需要较旧的版本时,只需转到其根文件夹并运行

pyenv local 2.7

There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions. Here is what I did to set up:

STEP1:

Remove all pythons from your mac

 brew uninstall --ignore-dependencies --force python
 sudo rm -rf ~/miniconda3/
 sudo rm -rf ~/.conda/

Remove the following from ~/.bash_profile

export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"

and also the following from ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"

STEP2:

Install pyenv and the python versions you need

brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0

STEP3:

add pyenv init to bash_profile or bashrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

STEP4:

Check what got installed

pyenv versions
  • system (set by /Users/ishandutta2007/.pyenv/version)

    2.7

    3.7.0

STEP5:

Choose a default

pyenv global 3.7.0

When a project needs older version, just go its root folder and run

pyenv local 2.7

回答 5

使用asdf

asdf的民谣

曾经有一种编程语言,
它有许多版本,
所以人们为它编写了一个版本管理器,以便
在项目的版本之间进行切换
,包括新旧版本。

然后出现了更多的编程语言,
所以出现了更多的版本管理器
以及许多针对他们的命令

我安装了很多,
我学到了很多命令

然后我说,
我将再写一个版本管理器

因此,出现了另一个版本管理器
asdf版本管理-https : //github.com/asdf-vm/asdf

版本管理器可扩展
,任何人都可以为其创建插件
以支持自己喜欢的语言,而
无需再安装更多版本管理器
或学习更多命令

https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins

Use asdf !

Ballad of asdf

Once upon a time there was a programming language
There were many versions of it
So people wrote a version manager for it
To switch between versions for projects
Different, old, new.

Then there came more programming languages
So there came more version managers
And many commands for them

I installed a lot of them
I learnt a lot of commands

Then I said, just one more version manager
Which I will write instead

So, there came another version manager
asdf version managerhttps://github.com/asdf-vm/asdf

A version manager so extendable
for which anyone can create a plugin
To support their favourite language
No more installing more version managers
Or learning more commands

https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins


回答 6

我以为我有相同的要求-在Python版本之间移动-但是通过从源代码构建而不是使用Python3.6实现了我所需的一切homebrew

git clone https://git.<theThingYouWantToInstall>

根据存储库,检查是否已经为该选项设置了MAKE文件。

I thought I had the same requirement – to move between Python versions – but I achieved all I needed with only Python3.6 by building from source instead of using homebrew.

git clone https://git.<theThingYouWantToInstall>

Depending on the repo, check if there is MAKE file already setup for this option.


回答 7

我能够去https://www.python.org/downloads/mac-osx/下载最新的python。它与当前的python一起安装在我的系统中。

I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. It installed along side current python in my system.


回答 8

好的,我正在努力安装Python3,因为我没有pip3

sudo pip3 command not found

所以我做到了

brew uninstall --force --ignore-dependencies python3

并从官方发行版中安装了常规的Python 3.6.2,然后安装了pip3,并且所有组件都正常。

Okay, I was struggling with my brew installation of Python3, because I didn’t have pip3

sudo pip3 command not found

and so I did

brew uninstall --force --ignore-dependencies python3

and installed the regular Python 3.6.2 from official distribution and then I had pip3 and all components were ok.


如何使用brew安装的Python作为默认Python?

问题:如何使用brew安装的Python作为默认Python?

我尝试在Mac OS X 10.6.2上切换到Homebrew(在使用了fink和macport之后)。我已经安装了python 2.7

brew install python 

问题是,与Macport相反,似乎没有python_select实用程序,而我的默认mac python始终是默认的

which python

给我吗

/usr/bin/python

而且/usr/bin/python不是符号链接

我如何才能使python brew成为我的默认python?

I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with

brew install python 

The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default

which python

give me

/usr/bin/python

and /usr/bin/python is not a symlink

How can I do to make python brew flavour to be my default python ?


回答 0

使用Homebrew时,以下命令可以提供更好的效果:

brew doctor

输出:

==> / usr / bin出现在/ usr / local / bin之前,这意味着将使用系统提供的程序代替Homebrew提供的程序。如果您例如这是一个问题。brew安装了Python。

考虑编辑.bash_profile,以将/ usr / local / bin放在$ PATH中的/ usr / bin之前。

As you are using Homebrew the following command gives a better picture:

brew doctor

Output:

==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. This is an issue if you eg. brew installed Python.

Consider editing your .bash_profile to put: /usr/local/bin ahead of /usr/bin in your $PATH.


回答 1

请参阅:如何在Homebrew中符号链接python?

$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python

See: How to symlink python in Homebrew?

$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python

回答 2

快速解决:

  1. 打开 /etc/paths
  2. 更改行的顺序(最高优先级在顶部)

就我而言/etc/paths

/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin

如果您想了解有关OSX中路径的更多信息,我发现本文非常有用:

http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/

Quick fix:

  1. Open /etc/paths
  2. Change the order of the lines (highest priority on top)

In my case /etc/paths looks like:

/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin

If you want to know more about paths in OSX I found this article quite useful:

http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/


回答 3

如自制软件安装程序本身所建议,请确保将其添加到您的.bashrc.zshrc

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

As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:

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

回答 4

我为OSX High Sierra做过“ brew install python”。该$PATH/usr/local/bin任何其他的路径之前,但仍然which python是指向系统的Python。

当我深入研究时,发现处没有python可执行文件/usr/local/bin。可执行文件名为python2。要解决此问题,请创建python指向以下内容的符号链接python2

/usr/local/bin $: ln -s python2 python

I did “brew install python” for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system’s python.

When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:

/usr/local/bin $: ln -s python2 python


回答 5

自制软件不会替换“ / usr / bin”中的内容。您只需要在路径中将“ / usr / local / bin”放在“ / usr / bin”之前,然后“哪个python”将为您提供“ / usr / local / bin / python”。

强烈建议不要替换/ usr / bin / python(或/ usr / bin / ruby​​)。

Homebrew does NOT replace stuff in “/usr/bin”. You’ll just want to put “/usr/local/bin” ahead of “/usr/bin” in your path, then “which python” will give you “/usr/local/bin/python”.

Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.


回答 6

python现在使用公式(目前python3为v3.6.5),brew将链接目录

/usr/local/opt/python -> ../Cellar/python/3.6.5

它还将链接二进制文件

/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3

如果仍然需要使用python2.x,请使用:

brew install python@2

要使用homebrew python,只需将其目录放在PATH中以进行bash:

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

对于鱼:

set -x PATH /usr/local/opt/python/libexec/bin $PATH

注意:

  1. 这样做会遮盖系统的默认版本 python
  2. 用于将python链接到/usr/local/share/python较旧版本的homebrew 。

python formula now uses python3(v3.6.5 for now), brew will link the directory:

/usr/local/opt/python -> ../Cellar/python/3.6.5

it will also link the binary:

/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3

If you still need to use python2.x, use:

brew install python@2

To use homebrew’s python, just put its directory in PATH, for bash:

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

for fish:

set -x PATH /usr/local/opt/python/libexec/bin $PATH

Note:

  1. doing this will shadow the system default version of python
  2. homebrew used to link python to /usr/local/share/python in older versions.

回答 7

修改$ PATH,将其添加到bashrc或bash_profile中:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

更多点击这里: 问题#89791

Modify your $PATH, Add this in your bashrc or bash_profile:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

more click here: Issue #89791


回答 8

您需要编辑PATH环境变量,以确保在/ usr / bin之前搜索自制python所在的位置。您还可以在shell配置中进行设置,以将诸如PYTHON之类的变量设置为所需的python版本并调用,$PYTHON而不是python从命令行调用。

另外,正如另一位发帖人所述(尤其是在Mac上),请勿弄乱/ usr / bin中的python将其指向另一个python安装。您只是在自找麻烦。

You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.

Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You’re just asking for trouble if you do.


回答 9

我所做的brew install python,我的$PATH还是不错的,不过,which python给我的系统中安装了一个。重新启动终端,将其修复。

I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.


回答 10

python现在指向python3,如果需要,python 2请执行以下操作: brew install python@2然后在.zshrc 或.bashrc文件中, export PATH="/usr/local/opt/python@2/libexec/bin:$PATH" 现在pyhon --version= Python 2.7.14和python3 --version= Python 3.6.4。这就是我在终端中经常看到的行为。

python now points to python3, if you need python 2 then do: brew install python@2 and then in your .zshrc or .bashrc file export PATH="/usr/local/opt/python@2/libexec/bin:$PATH" Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4. That’s the behavior I’m used to seeing in my terminal.


回答 11

我相信有一些方法可以将自制的python设置为默认值,但我认为解决问题的正确方法是不要弄乱系统python的路径:最好创建一个virtualenv,其中默认使用自制的python(通过使用virtualenv- -python选项)。使用类似的工具python_select几乎总是一个坏主意。

I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv –python option). Using tools like python_select is almost always a bad idea.


回答 12

改用pyenv在Python版本之间安装和切换。我已经使用rbenv多年了,但做的却是Ruby。在此之前,管理版本非常困难。

请查阅pyenv的github页面以获取安装说明。基本上是这样的:-使用自制软件安装pyenv。brew install pyenv -在shell启动脚本的末尾添加一个函数,以便pyenv可以做到。echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  • 使用pyenv安装所需的许多不同版本的Python。pyenv install 3.7.7
  • 将默认(全局)版本设置为刚安装的现代版本。pyenv global 3.7.7
  • 如果您从事的项目需要使用其他版本的python,请查看pyevn local。这将在项目的文件夹中创建一个文件,该文件指定python版本。Pyenv将使用该文件中的版本替换全局python版本。

Use pyenv instead to install and switch between versions of Python. I’ve been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.

Consult pyenv’s github page for installation instructions. Basically it goes like this: – Install pyenv using homebrew. brew install pyenv – Add a function to the end of your shell startup script so pyenv can do it’s magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  • Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
  • Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
  • If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project’s folder that specifies the python version. Pyenv will look override the global python version with the version in that file.

回答 13

做就是了:

brew install python
brew link python

之后,将其添加到您的bashrc或bash_profile中:

alias python='/usr/local/bin/python2'

请享用!

Just do:

brew install python
brew link python

After doing that, add this to your bashrc or bash_profile:

alias python='/usr/local/bin/python2'

Enjoy!


回答 14

/usr/local/opt/python/libexec/bin显式添加到您的.bash_profile

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

之后,它应该可以正常工作。

Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:

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

After that, it should work correctly.


回答 15

如果你是鱼壳

echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish

If you are fish shell

echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish

回答 16

您可以编辑/ etc / paths。这是我的:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

然后为python版本添加一个符号链接。就我而言

$ cd /usr/local/bin
$ ln -s python3 python

瞧!

You can edit /etc/paths. Here is mine:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Then add a symlink for the python version. In my case

$ cd /usr/local/bin
$ ln -s python3 python

Voila!


回答 17

从High Sierra开始,您需要使用:

sudo chown -R $(whoami) $(brew --prefix)/*

这是因为/usr/local不再受宠

Since High Sierra, you need to use:

sudo chown -R $(whoami) $(brew --prefix)/*

This is because /usr/local can no longer be chowned


回答 18

完全不知道默认Python是什么意思。我认为用其他版本替换系统Python解释器是一种不好的做法。系统功能可能在某种程度上取决于系统Python和特定模块或特定Python版本。相反,在一个安全的不同的地方安装自定义的Python的安装,并根据需要,以调用调整$ PATH 查找,而不是Python的通过路径的寻找默认的Python。

No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.


Virtualenvs中的参考损坏

问题:Virtualenvs中的参考损坏

我最近在Mac上安装了许多点文件以及其他一些应用程序(我改为使用iTerm而不是Terminal,将Sublime设置为默认文本编辑器),但是此后,尽管它们的文件夹位于.virtualenvs中,但我所有的虚拟环境都停止了工作仍然在那里,每当我尝试在其中运行任何命令时,它们都会给出以下错误:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
  Reason: image not found
Trace/BPT trap: 5

我已经删除了所有与dotfiles相关的文件,并将.bash_profile还原到以前的状态,但是问题仍然存在。是否有任何方法可以诊断问题或以简单的方式解决问题(例如,无需再次创建所有虚拟环境)?

I recently installed a bunch of dotfiles on my Mac along with some other applications (I changed to iTerm instead of Terminal, and Sublime as my default text editor) but ever since, all my virtual environments have stopped working, although their folders inside .virtualenvs are still there and they give the following error whenever I try to run anything in them:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
  Reason: image not found
Trace/BPT trap: 5

I have removed all the files related to dotfiles and have restored my .bash_profile to what it was before, but the problem persists. Is there any way to diagnose the problem or solve it in an easy way (e.g. not requiring to create all the virtualenvs all over again)?


回答 0

我在这里找到了解决问题的方法,所以所有功劳都归功于作者。

要点是,当您创建一个virtualenv时,会为安装了Homebrew的Python创建许多符号链接。

这是一个例子:

$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...

当您使用Homebrew升级Python然后运行时brew cleanup,virtualenv中的符号链接指向不再存在的路径(因为Homebrew删除了它们)。

符号链接需要指向新安装的Python:

lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python

解决方案是删除virtualenv中的符号链接,然后重新创建它们:

find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env

最好在删除链接之前先检查哪些链接将被删除:

find ~/.virtualenvs/my-virtual-env/ -type l

我认为,最好只删除损坏的符号链接。您可以使用GNU执行此操作find

gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete

find如果尚未安装GNU ,可以使用Homebrew 进行安装:

brew install findutils

请注意,默认情况下,随Homebrew一起安装的GNU程序通常以字母开头g。这是为了避免遮盖findOS X附带的二进制文件。

I found the solution to the problem here, so all credit goes to the author.

The gist is that when you create a virtualenv, many symlinks are created to the Homebrew installed Python.

Here is one example:

$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...

When you upgrade Python using Homebrew and then run brew cleanup, the symlinks in the virtualenv point to paths that no longer exist (because Homebrew deleted them).

The symlinks needs to point to the newly installed Python:

lrwxr-xr-x  1 ryan staff   78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python

The solution is to remove the symlinks in the virtualenv and then recreate them:

find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env

It’s probably best to check what links will be deleted first before deleting them:

find ~/.virtualenvs/my-virtual-env/ -type l

In my opinion, it’s even better to only delete broken symlinks. You can do this using GNU find:

gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete

You can install GNU find with Homebrew if you don’t already have it:

brew install findutils

Notice that by default, GNU programs installed with Homebrew tend to be prefixed with the letter g. This is to avoid shadowing the find binary that ships with OS X.


回答 1

在尝试了几件事之后,这对我有用:

转到您的virtualenv目录(但不要运行workon):

cd ~/.virtualenv/name_of_broken_venv

现在删除这些文件:

rm -rf .Python bin/python* lib/python2.7/* include/python2.7

然后重建您的venv,运行:

virtualenv .
workon name_of_broken_venv
pip freeze

现在,您应该再次看到已安装软件包的列表。

After trying a few things, this worked for me:

go to your virtualenv directory (but don’t run workon):

cd ~/.virtualenv/name_of_broken_venv

Now delete these files:

rm -rf .Python bin/python* lib/python2.7/* include/python2.7

Then to rebuild your venv, run:

virtualenv .
workon name_of_broken_venv
pip freeze

You should now see a list of your installed packages again.


回答 2

当我从Snow Leopard更新到Mac OS X Mavericks时,就会发生这种情况。我也必须事先重新安装brew。希望您使用pip为项目运行了冻结命令。

若要解决,您必须更新虚拟环境指向的路径。

  • 使用brew安装python版本:

brew install python

  • 重新安装virtualenvwrapper。

pip install --upgrade virtualenvwrapper

  • 删除了旧的虚拟环境:

rmvirtualenv old_project

  • 创建一个新的虚拟环境:

mkvirtualenv new_project

  • 在新的虚拟环境上工作

workon new_project

  • 使用pip安装新项目的要求。

pip install -r requirements.txt

这应该使项目保持以前的状态。

This occurred when I updated to Mac OS X Mavericks from Snow Leopard. I had to re-install brew beforehand too. Hopefully you ran the freeze command for your project with pip.

To resolve, you have to update the paths that the virtual environment points to.

  • Install a version of python with brew:

brew install python

  • Re-install virtualenvwrapper.

pip install --upgrade virtualenvwrapper

  • Removed the old virtual environment:

rmvirtualenv old_project

  • Create a new virtual environment:

mkvirtualenv new_project

  • Work on new virtual environment

workon new_project

  • Use pip to install the requirements for the new project.

pip install -r requirements.txt

This should leave the project as it was before.


回答 3

@Chris Wedgwood保留更新版本的答案site-packages(保留已安装的软件包)

cd ~/.virtualenv/name_of_broken_venv


mv lib/python2.7/site-packages ./    
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/

A update version @Chris Wedgwood‘s answer for keeping site-packages (keeping packages installed)

cd ~/.virtualenv/name_of_broken_venv


mv lib/python2.7/site-packages ./    
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/

回答 4

看来解决此问题的正确方法是运行

 pip install --upgrade virtualenv

用Homebrew升级python之后。

对于安装类似python的任何公式,该公式应该是一个通用过程,它具有自己的包管理系统。当您安装brew install python,在安装pythonpipeasy_installvirtualenv等。因此,如果这些工具可以自我更新,那么最好先尝试这样做,然后再将“自制”视为问题的根源。

It appears the proper way to resolve this issue is to run

 pip install --upgrade virtualenv

after you have upgraded python with Homebrew.

This should be a general procedure for any formula that installs something like python, which has it’s own package management system. When you install brew install python, you install python and pip and easy_install and virtualenv and so on. So, if those tools can be self-updated, it’s best to try to do so before looking to Homebrew as the source of problems.


回答 5

如果这是由brew upgrade升级其Python 引起的,并且可以降级到以前的版本,请尝试brew switch python [previous version],例如brew switch python 3.6.5从这里。

If this was caused by a brew upgrade that upgraded its Python, and you’re ok with downgrading to the previous version, try brew switch python [previous version], eg brew switch python 3.6.5. From here.


回答 6

virtualenvwrapper指令

如已接受的答案所示,根本原因可能是自制程序更新,这意味着您的virtualenv符号链接指向断开的python路径-请在此处查看详细信息。

对于每个虚拟环境,您需要重新分配符号链接以指向正确的python路径(在Brew酒窖中)。这是使用virtualenvwrapper进行操作的方法。在这里,我正在更新一个名为“ my-example-env”的虚拟环境。

cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env

全做完了。

virtualenvwrapper instructions

As indicated in the accepted answer, the root cause is likely a homebrew update that means your virtualenv symlinks are pointing at broken python paths – see details here.

For each virtual env, you need to reassign the symlinks to point at the correct python path (in brew cellar). Here is how to do it with virtualenvwrapper. Here I am updating a virtual env called “my-example-env”.

cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env

All done.


回答 7

任何使用pipenv的人(并且应该使用!)都可以简单地使用以下两个命令-无需激活venv:

rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev   # reinstall the venv from pipfile 

Anyone who is using pipenv (and you should!) can simply use these two commands — without having the venv activated:

rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev   # reinstall the venv from pipfile 

回答 8

如果您破坏了python3,请尝试brew upgrade python3为我修复它。

If you’ve busted python3 just try brew upgrade python3 that fixed it for me.


回答 9

我最近遇到了这个问题。以上解决方案均不适合我。看来这实际上不是Python的问题。当我运行

aws s3 ls

时,出现以下错误:

dyld: Library not loaded: @executable_path/../.Python

这意味着库aws可执行文件指向的是不存在或已损坏,因此我aws-cli按照此链接的说明进行了卸载并重新安装,并且有效!

I recently faced this. None of the above solutions worked for me. Seems it wasn’t actually Python’s problem. When I was running

aws s3 ls

I was getting following error:

dyld: Library not loaded: @executable_path/../.Python

This means, the library aws executable is pointing towards is either doesn’t exist or is corrupted, thus I uninstalled and reinstalled aws-cli following instructions from this link and it worked!!


回答 10

我(MacOS用户)遇到的问题是,brew将Python和virtualenvs链接更新为已删除的旧版本。

我们可以通过以下方式进行检查和修复

>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s  /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python

The problem for me(a MacOS user) is that brew updated the Python and virtualenvs links to the old version which was deleted.

We can check and fix it by

>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s  /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python

回答 11

我有一个类似的问题,我只是通过使用 virtualenv .

I had a similar issue and i solved it by just rebuilding the virtual environment with virtualenv .


回答 12

使用Python 2.7.10。

一个命令即可virtualenv path-to-env完成。文件资料

$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.

Using Python 2.7.10.

A single command virtualenv path-to-env does it. documentation

$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.

回答 13

由于python的Homebrew重新安装(因此损坏的符号链接)以及我之前完成的一些“ sudo pip安装”,我的虚拟环境也损坏了。Weizhong的技巧对解决问题非常有用,而无需重新安装软件包。对于混合权限问题,我还必须执行以下操作。

须藤chown -R my_username lib / python2.7 / site-packages

I had a broken virtual env due to a Homebrew reinstall of python (thereby broken symlinks) and also a few “sudo pip install”s I had done earlier. Weizhong’s tips were very helpful in fixing the issues without having to reinstall packages. I also had to do the following for the mixed permissions problem.

sudo chown -R my_username lib/python2.7/site-packages


回答 14

Virtualenvs已损坏。有时,简单的方法是删除venv文件夹并重新创建virutalenvs。

Virtualenvs are broken. Sometimes simple way is to delete venv folders and recreate virutalenvs.


回答 15

如果您使用pipenv,只需pipenv --rm解决即可。

If you using pipenv, just doing pipenv --rm solves the problem.


回答 16

在OSX Catalina上升级brew后,我遇到了同样的问题。

在尝试了一堆东西之后,我发现以下是最佳和简便的解决方案。

首先,删除虚拟环境。(可选的)

find myvirtualenv -type l -delete

然后重新创建一个新的virtualenv

virtualenv myvirtualenv

参考:https : //www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/

I was facing the same issue after upgrading brew on my OSX Catalina.

After trying bunch of stuffs, I find the following is the best and easy solution.

At first, delete the virtual env. (Optional)

find myvirtualenv -type l -delete

then recreate a new virtualenv

virtualenv myvirtualenv

Reference: https://www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/


回答 17

接受的答案对我不起作用:该文件$WORKON_HOME/*/bin/python2.7不再是符号链接,而是功能全面的可执行文件:

$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...

a,解决方案是完全删除所有虚拟环境并从头开始重新创建。

供参考:

deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
  # assume that each VE is associated with a project
  # and the project has the requirements.txt file
  project=$(cat $WORKON_HOME/$ve/.project)
  rmvirtualenv $ve
  mkvirtualenv -a $project -r requirements.txt $ve
done

The accepted answer does not work for me: the file $WORKON_HOME/*/bin/python2.7 is no longer a symlink, it is a full-fledged executable:

$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...

The solution is, alas, to completely remove and re-create from scratch all the virtual environments.

For the reference:

deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
  # assume that each VE is associated with a project
  # and the project has the requirements.txt file
  project=$(cat $WORKON_HOME/$ve/.project)
  rmvirtualenv $ve
  mkvirtualenv -a $project -r requirements.txt $ve
done

回答 18

简单地升级python3对我有用:

brew upgrade python3

Simply upgrading python3 worked for me:

brew upgrade python3

回答 19

我尝试了前几种方法,但对我而言,它们却无济于事,这些方法正试图使毒素发挥作用。最终有效的是:

sudo pip install tox

即使已经安装了tox。输出终止于:

Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0

I tried the top few methods, but they didn’t work, for me, which were trying to make tox work. What eventually worked was:

sudo pip install tox

even if tox was already installed. The output terminated with:

Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0

回答 20

对我来说,解决此问题的方法只是卸载python3和pipenv,然后重新安装它们。

brew uninstall pipenv
brew uninstall python3
brew install python3 
brew install pipenv

What fixed it for me was just uninstalling python3 and pipenv then reinstalling them.

brew uninstall pipenv
brew uninstall python3
brew install python3 
brew install pipenv

回答 21

这里所有的答案都很棒,我尝试了Ryan,Chris上面提到的几种解决方案,但无法解决问题,因此必须采取快速而肮脏的方法。

  1. rm -rf <project dir>(或者mv <project dir> <backup projct dir>如果您想保留备份)
  2. git clone <project git url>
  3. 继续!

这里没有什么新奇的东西,但是它使生活更轻松!

All the answers are great here, I tried a couple of solutions mentioned above by Ryan, Chris and couldn’t resolve the issue, so had to follow a quick and dirty way.

  1. rm -rf <project dir> (or mv <project dir> <backup projct dir> if you want to keep a backup)
  2. git clone <project git url>
  3. Move on!

Nothing novel here, but it makes life easier!


回答 22

我确定我晚会晚了,但是我想说,解决这个问题比这里讨论的要简单得多。

您可以轻松地重新生成虚拟环境,而无需删除/编辑任何内容。假设您调用了损坏的环境,则env_to_fix可以执行以下操作:

mkvirtualenv env_to_fix

这将重新生成链接并修复环境,而无需将当前状态转储到某个位置并进行恢复。

I am sure I am late to the party but I want to say that the resolution of this problem is much simpler than discussed here.

You can easily regenerate the virtual environment without having to delete/edit anything. Assuming that your broken environment is called env_to_fix you can just to the following:

mkvirtualenv env_to_fix

This will regenerate the links and fix the environment without the need to dump the current status somewhere and restore it.


回答 23

当我在Mac上将python运行时从2指向3时,遇到了相同的问题,将别名python指向python 3路径。然后,我重新创建一个新的virtualenv并重新安装我的项目所需的那些软件包。对于我的用例,我有一个写给Google工作表的python程序。清理一些与python 2实现不同的程序包,然后一切又开始起作用。

I came across the same issue when I was pointing my python run time from 2 to 3 on my mac, pointing the alias python to python 3 path. I then recreate a new virtualenv and re-install those packages i need for my project. For my use case i have had a python program writing to google sheet. Clean up a few packages that are different from python 2 implementation and wa la, things started working again.