在终端中激活不同的 Python 版本 [英] Activate different Python version in Terminal

查看:101
本文介绍了在终端中激活不同的 Python 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚将我的 Mac 重新格式化为 Yosemite 并安装了 Python 2 &3 使用自制软件.我还在我的 bash_profile 文件中设置了一些别名,这允许我激活我想要使用的 Python 版本.所以如果我输入 p3,它会启动 python3 shell.

Just reformatted my Mac to Yosemite and installed Python 2 & 3 using Homebrew. I've also setup some aliases in my bash_profile file which allows me to activate which version of Python I want to use. So if I type p3, it'll launch the python3 shell.

我想知道是否可以在不启动 Python shell 的情况下激活特定版本的 Python?例如,如果我将任何文件带入终端,它将使用我激活的版本?

I'm wondering if it's possible to activate a specific version of Python without it starting the Python shell? So if I bring any files into the Terminal for instance, it'll use the version which I've activated?

干杯!

抱歉,如果在其他地方已经回答了这个问题,我进行了很好的搜索,但找不到任何内容.

Apologies if this has been answered elsewhere, I had a good search but I couldn't find anything.

推荐答案

与其编写自己的脚本来管理不同的 Python 版本,我建议使用已经过社区测试的高度使用的管理器:<强>pyenv.使用 pyenv,您可以:

Rather than writing your own scripts to manage different Python versions, I would suggest using a highly-used manager that has been tested in and out by the community: pyenv. With pyenv you can:

  1. 从命令行轻松安装几个不同的 Python 版本,没有任何问题(pyenv install 3.4.2)

如果您想划分可用的包(pyenv virtualenv 3.4.2 mypy3projectvenv),请从这些版本中的任何一个创建虚拟环境,以及

Create virtual environments from any one of those versions if you want to compartmentalize the packages that are available (pyenv virtualenv 3.4.2 mypy3projectvenv), and

  • 全局(pyenv global [version-or-venv]),
  • 本地在您配置的文件夹中和下方(pyenv local [ver-or-venv]),通常对您在特定版本/virtualenv 中拥有的项目有用,以及
  • 本地到外壳直到关闭(pyenv shell [ver-or-venv])(这可能与您假定的 p3 命令最相似.
  • globally (pyenv global [version-or-venv]),
  • locally in and below folders you configure (pyenv local [ver-or-venv]), usually useful for projects that you have at a specific version/virtualenv, and
  • local to the shell until closed (pyenv shell [ver-or-venv]) (this is perhaps most-similar to your putative p3 command.

设置好要使用的 Python 后,所有与 Python 相关的调用都将重定向到其适当的目标(例如 pythonpipeasy_install>、ipython*、django-admin*).不要使用任何特殊命令执行 Python 脚本,只需正常调用它们(或在标准 #!/usr/bin/env python shebang 前加上前缀)

After setting the Python you want to use, all Python-related calls are redirected to their appropriate target (e.g. python, pip, easy_install, ipython*, django-admin*). Don't execute the Python scripts with any special command, just call them normally (or prefix a standard #!/usr/bin/env python shebang)

*如果安装在那个版本/virtualenv

如果您使用 pyenv-installer 脚本:

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

然后将它告诉您的几行添加到您的 ~/.bash_profile 脚本中(然后获取它或重新启动 bash)...您将在几秒钟内启动并运行.诀窍通常是使用 brew(sqlite、OpenSSH、zlib...)安装所有 Python 构建依赖项,但在那之后,您就大功告成了.

Then add the couple lines it tells you to your ~/.bash_profile script (and either source it or restart bash)...you'll be up and running in seconds. The trick is usually installing all the Python build dependencies with brew (sqlite, OpenSSH, zlib...), but after that then you're golden.

这篇关于在终端中激活不同的 Python 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆