mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
translated
This commit is contained in:
parent
7dd3bab487
commit
b046f4ff32
@ -1,186 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use pyenv to run multiple versions of Python on a Mac)
|
||||
[#]: via: (https://opensource.com/article/20/4/pyenv)
|
||||
[#]: author: (Matthew Broberg https://opensource.com/users/mbbroberg)
|
||||
|
||||
How to use pyenv to run multiple versions of Python on a Mac
|
||||
======
|
||||
If you need to run a project that uses a Python version you don't have
|
||||
installed on macOS, try pyenv.
|
||||
![Searching for code][1]
|
||||
|
||||
Managing a local Python development environment continues to be a challenge, even for experienced developers. While there are well-documented [strategies for package management][2], there is another step necessary to ensure you are running the version of Python you need when you need it.
|
||||
|
||||
### Why does the version of Python matter?
|
||||
|
||||
It's a strange concept at first, but programming languages change like any other software. They have bugs, fixes, and updates like any of your favorite [APIs][3] and any other software. Similarly again, different releases are identified by a three-digit number known as a [semantic version][4].
|
||||
|
||||
> 😭😭😭 [pic.twitter.com/yt1Z2439W8][5]
|
||||
>
|
||||
> — Denny Perez (@dennyperez18) [May 28, 2019][6]
|
||||
|
||||
For many years, Python 2 was the commonly used major version of the programming language. In January 2020, Python 2 [reached end of life][7], and only Python 3 will be supported by the language's core maintainers from then forward. Python 3 is developing steadily, and releasing new updates regularly. That makes it important for me to regularly get those updates.
|
||||
|
||||
Recently, I tried to run a project on macOS that depended on Python 3.5.9, a version that I did not have installed on my system. It might seem logical to think the Python package manager **pip** could install it*, but that wasn't the case:
|
||||
|
||||
|
||||
```
|
||||
$ pip install python3.5.9
|
||||
Collecting python3.5.9
|
||||
ERROR: Could not find a version that satisfies the requirement python3.5.9 (from versions: none)
|
||||
ERROR: No matching distribution found for python3.5.9
|
||||
```
|
||||
|
||||
Alternatively, I could have downloaded that version from the official Python website, but how would I run it in on my Mac alongside my existing version of Python? Specifying the version of Python I intend to use every time I run the interpreter (python3.7 or python3.5 for example) seems error-prone at best. There has to be a better way.
|
||||
|
||||
_(A note on the above: I know this makes no sense to seasoned Python developer, but it made sense to me at the time. I would happily talk about why I still think it should.)_
|
||||
|
||||
### Installing and setting up pyenv
|
||||
|
||||
Thankfully, **pyenv** exists to work around this series of complexities. To start, I needed to install pyenv. I could clone and compile it myself [from source][8], but I prefer to manage packages like this through the Homebrew package manager:
|
||||
|
||||
|
||||
```
|
||||
`$ brew install pyenv`
|
||||
```
|
||||
|
||||
In order to use the version of Python through pyenv, it's essential to understand the shell's PATH variable. PATH determines where the shell searches for files by the name of the command. You must ensure the shell will find the version of Python run by pyenv, not the one installed by default (which is often called the _system version_). If you don't change the path, here is the result:
|
||||
|
||||
|
||||
```
|
||||
$ which python
|
||||
/usr/bin/python
|
||||
```
|
||||
|
||||
That's the system version of Python.
|
||||
|
||||
To set up pyenv correctly, you can run the following in Bash or zsh:
|
||||
|
||||
|
||||
```
|
||||
`$ PATH=$(pyenv root)/shims:$PATH`
|
||||
```
|
||||
|
||||
Now, if you check the version of Python, you'll see it is the one managed by pyenv:
|
||||
|
||||
|
||||
```
|
||||
$ which python
|
||||
/Users/my_username/.pyenv/shims/python
|
||||
```
|
||||
|
||||
That export statement (PATH=) will only change for this shell instance, so make it a permanent change, you need to add it to your dotfiles. Since zsh is officially macOS's default shell, I'll focus on it. Append that same syntax to the **~/.zshrc** file:
|
||||
|
||||
|
||||
```
|
||||
`$ echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc`
|
||||
```
|
||||
|
||||
Now every time we run a command in zsh, it will use the pyenv version of Python. Note that I used single quotes with **echo** so it does not evaluate and expand the commands.
|
||||
|
||||
The .zshrc file only manages zsh instances, so be sure to check what your shell is and edit the associated dotfiles. If you need to double-check what your default shell is, you can run **echo $SHELL**. If it's zsh, use the command above. If you use Bash, change **~/.zshrc** to **~/.bashrc**. You can dive deep into [path setting][9] in pyenv's README if you would like to learn more.
|
||||
|
||||
### Using pyenv to manage Python versions
|
||||
|
||||
Now that pyenv is in control, we can see it only has the system Python available to it:
|
||||
|
||||
|
||||
```
|
||||
$ pyenv versions
|
||||
system
|
||||
```
|
||||
|
||||
As mentioned above, you absolutely do not want to use this version ([read more on why][10]). Now that pyenv is set up correctly, I want it to have a few different versions of Python that I regularly use.
|
||||
|
||||
There is a way to see all Python versions available from all the different repositories pyenv has access to by running **pyenv install --list**. It's a long, overwhelming list that may be helpful to review in the future. For now, I stick with the latest of each dot-release (3.5.x or 3.6.x where x is the latest) found on the [Python download page][11]. With that in mind, I'll install 3.5.9 and 3.8.0:
|
||||
|
||||
|
||||
```
|
||||
$ pyenv install 3.5.9
|
||||
$ pyenv install 3.8.0
|
||||
```
|
||||
|
||||
This will take a while, so get some tea (or read one of the links above). It's interesting to note that the output walks through the download and building of that version of Python. For example, the output shows that the file comes directly from [Python.org][12].
|
||||
|
||||
Once everything is installed, you can set up your defaults. I like to live at the cutting edge, so I set my global default Python version to the latest:
|
||||
|
||||
|
||||
```
|
||||
`$ pyenv global 3.8.0`
|
||||
```
|
||||
|
||||
And that version is immediately set in my shell. To confirm:
|
||||
|
||||
|
||||
```
|
||||
$ python -V
|
||||
Python 3.8.0
|
||||
```
|
||||
|
||||
The project I want to run works only with Python 3.5, so I'll set the version locally and confirm it's in use:
|
||||
|
||||
|
||||
```
|
||||
$ pyenv local 3.5.9
|
||||
$ python -V
|
||||
Python 3.5.9
|
||||
```
|
||||
|
||||
Because I used the **local** option with pyenv, it added a file to my current directory to track that information.
|
||||
|
||||
|
||||
```
|
||||
$ cat .python-version
|
||||
3.5.9
|
||||
```
|
||||
|
||||
Now, I can finally set up a virtual environment for the project I want and be sure I'm running the right version of Python.
|
||||
|
||||
|
||||
```
|
||||
$ python -m venv venv
|
||||
$ source ./venv/bin/activate
|
||||
(venv) $ which python
|
||||
/Users/mbbroberg/Develop/my_project/venv/bin/python
|
||||
```
|
||||
|
||||
To learn more, check out this tutorial about [managing virtual environments on a Mac][13].
|
||||
|
||||
### Wrapping up
|
||||
|
||||
By default, running multiple Python versions can be a challenge. I find starting with pyenv ensures I have the versions of Python I need set up to run when I need them.
|
||||
|
||||
Do you have other beginner or intermediate Python questions? Leave a comment, and we will consider them for a future article.
|
||||
|
||||
Newcomers to python-ideas occasionally make reference to the idea of "Python 4000" when proposing...
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/4/pyenv
|
||||
|
||||
作者:[Matthew Broberg][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/mbbroberg
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/search_find_code_python_programming.png?itok=ynSL8XRV (Searching for code)
|
||||
[2]: https://opensource.com/article/19/4/managing-python-packages
|
||||
[3]: https://opensource.com/article/19/5/api-evolution-right-way
|
||||
[4]: https://semver.org/
|
||||
[5]: https://t.co/yt1Z2439W8
|
||||
[6]: https://twitter.com/dennyperez18/status/1133505310516232203?ref_src=twsrc%5Etfw
|
||||
[7]: https://opensource.com/article/19/11/end-of-life-python-2
|
||||
[8]: https://github.com/pyenv/pyenv
|
||||
[9]: https://github.com/pyenv/pyenv#understanding-path
|
||||
[10]: https://opensource.com/article/19/5/python-3-default-mac
|
||||
[11]: https://www.python.org/downloads/
|
||||
[12]: http://python.org
|
||||
[13]: https://opensource.com/article/19/6/python-virtual-environments-mac
|
@ -0,0 +1,180 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use pyenv to run multiple versions of Python on a Mac)
|
||||
[#]: via: (https://opensource.com/article/20/4/pyenv)
|
||||
[#]: author: (Matthew Broberg https://opensource.com/users/mbbroberg)
|
||||
|
||||
如何在 Mac 上使用 pyenv 运行多个版本的 Python
|
||||
======
|
||||
如果你需要运行 macOS 上没有安装的 Python 版本,请试试 pyenv。
|
||||
![Searching for code][1]
|
||||
|
||||
即使对于有经验的开发人员,管理本地 Python 开发环境仍然是一个挑战。尽管有详细的[软件包管理策略][2],但仍需要采取另外的步骤来确保你在需要时运行所需的 Python 版本。
|
||||
|
||||
### 为什么 Python 版本重要?
|
||||
|
||||
起初这是一个奇怪的概念,但是编程语言会像其他任何软件一样发生变化。它们有 bug,修复和更新,就像你喜欢的 [API][3] 和任何其他软件一样。同样,不同的发行版由称为[语义版本][4]的三位数标识。
|
||||
|
||||
> 😭😭😭 [pic.twitter.com/yt1Z2439W8][5]
|
||||
>
|
||||
> — Denny Perez (@dennyperez18) [May 28, 2019][6]
|
||||
|
||||
多年来,Python 2 是该语言的常用主要版本。在 2020 年 1 月,Python 2 [到达最后寿命][7],此后,Python 的核心维护者将仅支持 Python 3。Python 3 稳步发展,并定期发布新更新。对我来说定期获取这些更新很重要。
|
||||
|
||||
最近,我尝试在依赖于 Python 3.5.9 的 macOS 上运行一个项目,但该版本尚未安装在系统上。我认为 Python 包管理器 **pip** 可以安装它,但事实并非如此:
|
||||
|
||||
|
||||
```
|
||||
$ pip install python3.5.9
|
||||
Collecting python3.5.9
|
||||
ERROR: Could not find a version that satisfies the requirement python3.5.9 (from versions: none)
|
||||
ERROR: No matching distribution found for python3.5.9
|
||||
```
|
||||
|
||||
或者,我可以从官方 Python 网站下载该版本,但是除了现有的 Python 版本外,如何在 Mac 上运行它?每次运行指定 Python 解释器版本(例如 python3.7 或 python3.5)似乎很容易出错。一定会有更好的方法。
|
||||
|
||||
_(说明:我知道这对经验丰富的 Python 开发人员没有意义,但对当时的我来说是有意义的。我很乐意谈论为什么我仍然认为应该如此。)_
|
||||
|
||||
### 安装和设置 pyenv
|
||||
|
||||
值得庆幸的是,**pyenv** 可以解决这一系列复杂性。首先,我需要安装 pyenv。我可以[从源码][8]自己克隆并编译它,但是我更喜欢通过 Homebrew 包管理器来管理软件包:
|
||||
|
||||
```
|
||||
`$ brew install pyenv`
|
||||
```
|
||||
|
||||
为了通过 pyenv 使用 Python 版本,必须了解 shell 的 PATH 变量。PATH 通过命令名称确定 shell 在哪里搜索文件。你必须确保 shell 程序能够找到 pyenv 运行的 Python 版本,而不是默认安装的版本(通常称为_系统版本_)。如果不更改路径,那么结果如下:
|
||||
|
||||
|
||||
```
|
||||
$ which python
|
||||
/usr/bin/python
|
||||
```
|
||||
|
||||
这是 Python 的系统版本。
|
||||
|
||||
要正确设置 pyenv,可以在 Bash 或 zsh 中运行以下命令:
|
||||
|
||||
```
|
||||
`$ PATH=$(pyenv root)/shims:$PATH`
|
||||
```
|
||||
|
||||
现在,如果你检查 Python 的版本,你会看到它是 pyenv 管理的版本:
|
||||
|
||||
|
||||
```
|
||||
$ which python
|
||||
/Users/my_username/.pyenv/shims/python
|
||||
```
|
||||
|
||||
该 export 语句(PATH=)仅会对该 shell 实例进行更改,为了永久更改,你需要将它添加到 dotfile 中。由于 zsh 是 macOS 的默认 shell,因此我将重点介绍它。将相同的语法添加到 **~/.zshrc** 文件中:
|
||||
|
||||
|
||||
```
|
||||
`$ echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc`
|
||||
```
|
||||
|
||||
现在,每次我们在 zsh 中运行命令时,它将使用 pyenv 版本的 Python。请注意,我在 **echo** 中使用了单引号,因此它不会评估和扩展命令。
|
||||
|
||||
.zshrc 文件仅管理 zsh 实例,因此请确保检查你的 shell 程序并编辑关联的 dotfile。如果需要再次检查默认 shell 程序,可以运行 **echo $SHELL**。如果是 zsh,请使用上面的命令。如果你使用 Bash,请将 **~/.zshrc** 更改为 **~/.bashrc**。如果您想了解更多信息,可以在pyenv的自述文件中深入研究[path setting] [9]。
|
||||
|
||||
### 使用 pyenv 管理 Python 版本
|
||||
|
||||
现在 pyenv 已经可用,我们可以看到它只有系统 Python 可用:
|
||||
|
||||
```
|
||||
$ pyenv versions
|
||||
system
|
||||
```
|
||||
|
||||
如上所述,你绝对不想使用此版本([阅读更多有关信息][10])。现在 pyenv 已正确设置,我希望它有我经常使用的几个不同版本的 Python。
|
||||
|
||||
有一种方法可以通过运行 **pyenv install --list** 来查看 pyenv 可以访问的所有不同仓库中的所有 Python 版本。这是一个很长的列表,将来可能会有所帮助。目前,我决定在 [Python 下载页面][11]找到的每个最新的“点版本”(3.5.x 或 3.6.x,其中 x 是最新的)。因此,我将安装 3.5.9 和 3.8.0:
|
||||
|
||||
|
||||
```
|
||||
$ pyenv install 3.5.9
|
||||
$ pyenv install 3.8.0
|
||||
```
|
||||
|
||||
这将需要一段时间,因此休息一会(或阅读上面的链接之一)。有趣的是,输出遍历了该版本的 Python 的下载和构建。例如,输出显示文件直接来自 [Python.org][12]。
|
||||
|
||||
安装完成后,你可以设置默认值。我喜欢最新的,因此将全局默认 Python 版本设置为最新版本:
|
||||
|
||||
|
||||
```
|
||||
`$ pyenv global 3.8.0`
|
||||
```
|
||||
|
||||
该版本立即在我的 shell 中设置完成。要确认:
|
||||
|
||||
|
||||
```
|
||||
$ python -V
|
||||
Python 3.8.0
|
||||
```
|
||||
|
||||
我要运行的项目仅适于 Python 3.5,因此我将在本地设置该版本并确认:
|
||||
|
||||
|
||||
```
|
||||
$ pyenv local 3.5.9
|
||||
$ python -V
|
||||
Python 3.5.9
|
||||
```
|
||||
|
||||
因为我在 pyenv 中使用了 **local** 选项,所以它向当前目录添加了一个文件来跟踪该信息。
|
||||
|
||||
|
||||
```
|
||||
$ cat .python-version
|
||||
3.5.9
|
||||
```
|
||||
|
||||
现在,我终于可以为想要的项目设置虚拟环境,并确保运行正确版本的 Python。
|
||||
|
||||
|
||||
```
|
||||
$ python -m venv venv
|
||||
$ source ./venv/bin/activate
|
||||
(venv) $ which python
|
||||
/Users/mbbroberg/Develop/my_project/venv/bin/python
|
||||
```
|
||||
|
||||
要了解更多信息,请查看有关[在 Mac 上管理虚拟环境][13]的教程。
|
||||
|
||||
### 总结
|
||||
|
||||
默认情况下,运行多个 Python 版本可能是一个挑战。我发现 pyenv 可以确保在我需要时可以有我需要的 Python 版本。
|
||||
|
||||
你还有其他初学者或中级 Python 问题吗? 请发表评论,我们将在以后的文章中考虑它们。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/4/pyenv
|
||||
|
||||
作者:[Matthew Broberg][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/mbbroberg
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/search_find_code_python_programming.png?itok=ynSL8XRV (Searching for code)
|
||||
[2]: https://opensource.com/article/19/4/managing-python-packages
|
||||
[3]: https://opensource.com/article/19/5/api-evolution-right-way
|
||||
[4]: https://semver.org/
|
||||
[5]: https://t.co/yt1Z2439W8
|
||||
[6]: https://twitter.com/dennyperez18/status/1133505310516232203?ref_src=twsrc%5Etfw
|
||||
[7]: https://opensource.com/article/19/11/end-of-life-python-2
|
||||
[8]: https://github.com/pyenv/pyenv
|
||||
[9]: https://github.com/pyenv/pyenv#understanding-path
|
||||
[10]: https://opensource.com/article/19/5/python-3-default-mac
|
||||
[11]: https://www.python.org/downloads/
|
||||
[12]: http://python.org
|
||||
[13]: https://opensource.com/article/19/6/python-virtual-environments-mac
|
Loading…
Reference in New Issue
Block a user