运行多个Python版本

发布于 2024-09-26 13:33:50 字数 148 浏览 5 评论 0原文

我想在我的机器上运行多个 Python 版本。有没有 Python 版本管理器,我可以在多个 Python 版本之间切换,而无需调用 Python 二进制文件的完整路径?我尝试过 virtualenv,它似乎只涵盖运行多个 Python 库版本的问题。

感谢您的帮助。

I want to run multiple Python versions in my box. Is there any version manager for Python where I can switch between multiple Python versions without having to call the full path of the Python binary? I have tried virtualenv and it seems to only cover problems running multiple Python library versions.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵山崖 2024-10-03 13:33:50

从 bash 调用 python 时,您可以尝试使用别名。

user@machine:~$ alias python1234='/usr/bin/python2.5'
user@machine:~$ python1234
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

假设您有一个名为 script.py 的脚本,其中包含以下内容:

import sys
print sys.version

因此,使用不同版本的 python 启动脚本如下所示:

user@machine:~$ python script.py 
2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3]
user@machine:~$ python1234 script.py 
2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3]

When calling python from bash you could try an alias.

user@machine:~$ alias python1234='/usr/bin/python2.5'
user@machine:~$ python1234
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Let's say you have a script called script.py with following content:

import sys
print sys.version

So, launching a script with a different version of python looks like:

user@machine:~$ python script.py 
2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3]
user@machine:~$ python1234 script.py 
2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3]
筑梦 2024-10-03 13:33:50

我使用 virtualenv 来跟踪项目所需的不同环境。我可以在一个环境中设置 django 1.0,或者在另一个环境中设置 django 1.2。您也可以使用它来设置您想在特定环境中使用哪个版本的 python。以下是该网站的链接,该网站提供了有关如何运行的出色示例和教程: http://pypi.python .org/pypi/virtualenv

I use virtualenv to keep track of different environments I need for my projects. I may setup django 1.0 in one environment or django 1.2 for another. You can use it to set which version of python you'd like to use in a particular environment as well. Here's the link to the site which has great samples and tutorials for how to get running: http://pypi.python.org/pypi/virtualenv

|煩躁 2024-10-03 13:33:50

您不必使用完整路径。

user@machine:$ python2.5
Python 2.5.5 (r255:77872, Sep 14 2010, 17:16:34) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

user@machine:$ python2.6
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

这能回答你的问题吗?

You don't have to use the full path.

user@machine:$ python2.5
Python 2.5.5 (r255:77872, Sep 14 2010, 17:16:34) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

user@machine:$ python2.6
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Does that answer your question?

以酷 2024-10-03 13:33:50

安装多个解释器版本

node-build安装多个 Node 版本的常用工具。

node-build 安装:

brew install node-build

node-build 用法:

node-build 14.20.1 ~/.nodes/node-14.20.1
node-build 16.17.1 ~/.nodes/node-16.17.1

python-build安装多个Python版本的常用工具。

python-build 安装:

git clone https://github.com/pyenv/pyenv.git
cd pyenv/plugins/python-build
./install.sh

python-build 用法:

python-build 3.8.10 ~/.pythons/python-3.8.10
python-build 3.9.13 ~/.pythons/python-3.9.13

管理多个解释器版本

rvmrbenvchruby 是管理多个 Ruby 版本的常用工具。他们启发了 nvmnodenvchnode,它们是管理多个 Node 版本的等效工具。他们还启发了 pyenvchpython,它们是用于管理多个 Python 版本的等效工具(没有与 rvm 等效的工具)据我所知)。

调用 Ruby 命令时(rubyirb)自动切换位于已安装 Ruby 版本的 bin 目录中的目标可执行文件rakegem、…)根据当前或最近父目录的 .ruby-version 文件中定义的 Ruby 版本,这些工具使用不同的策略(从最重到最轻) ):

  • rvm 通过重新定义 cd 命令,在每次调用 cd 命令之前更新 PATH 环境变量;
  • rbenv 通过执行代理可执行文件 (垫片);
  • chruby 在每次调用命令之前更新 PATH 环境变量,方法是执行使用 preexec_functions(对于 Zsh)和 PROMPT_COMMAND 挂钩到 shell 的函数 用于 Bash。

rvm 策略的缺点是它是侵入性的且缓慢。 rbenv 策略的缺点是它很复杂,并且需要在每次安装 Ruby 版本或 Ruby 包(gem< /em>) 提供可执行文件。 chruby 策略不存在这些问题,因此 chruby 是推荐的工具。

chnode 安装:

brew tap tkareine/chnode
brew install tkareine/chnode/chnode
echo "source /usr/local/opt/chnode/share/chnode/chnode.sh" >>~/.zshrc
echo "source /usr/local/opt/chnode/share/chnode/auto.sh" >>~/.zshrc
echo "precmd_functions+=(chnode_auto)" >>~/.zshrc

chnode 用法:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#    node-16.17.1
chnode 16.17.1
echo $PATH
# /Users/me/.nodes/node-16.17.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#  * node-16.17.1
echo 14.20.1 >.node-version
echo $PATH
# /Users/me/.nodes/node-14.20.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#  * node-14.20.1
#    node-16.17.1

chpython 安装:

git clone https://github.com/kisoku/chpython.git
cd chpython
make install
echo "source /usr/local/share/chpython/chpython.sh" >>~/.zshrc
echo "source /usr/local/share/chpython/auto.sh" >>~/.zshrc

chpython 用法:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#    python-3.9.13
chpython 3.9.13
echo $PATH
# /Users/me/.pythons/python-3.9.13/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#  * python-3.9.13
echo 3.8.10 >.python-version
echo $PATH
# /Users/me/.pythons/python-3.8.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#  * python-3.8.10
#    python-3.9.13

Installing Multiple Interpreter Versions

node-build is a common tool for installing multiple Node versions.

node-build installation:

brew install node-build

node-build usage:

node-build 14.20.1 ~/.nodes/node-14.20.1
node-build 16.17.1 ~/.nodes/node-16.17.1

python-build is a common tool for installing multiple Python versions.

python-build installation:

git clone https://github.com/pyenv/pyenv.git
cd pyenv/plugins/python-build
./install.sh

python-build usage:

python-build 3.8.10 ~/.pythons/python-3.8.10
python-build 3.9.13 ~/.pythons/python-3.9.13

Managing Multiple Interpreter Versions

rvm, rbenv, and chruby are common tools for managing multiple Ruby versions. They inspired nvm, nodenv, and chnode which are equivalent tools for managing multiple Node versions. They also inspired pyenv and chpython which are equivalent tools for managing multiple Python versions (there is no rvm equivalent as far as I know).

To automatically switch the targeted executables located in the bin directories of the installed Ruby versions when invoking a Ruby command (ruby, irb, rake, gem, …) based on the Ruby version defined in the .ruby-version file of the current or nearest parent directory, the tools use different strategies (from the heaviest to the lightest):

  • rvm updates the PATH environment variable before each invocation of the cd command by redefining the cd command;
  • rbenv invokes the Ruby command in a dedicated environment with an updated PATH environment variable by executing a proxy executable (shim);
  • chruby updates the PATH environment variable before each invocation of a command by executing a function hooked into the shell with preexec_functions for Zsh and PROMPT_COMMAND for Bash.

The drawback of the rvm strategy is that it is invasive and slow. The drawback of the rbenv strategy is that it is complex and requires shims to be regenerated (rehashed) at each installation of a Ruby version or Ruby package (gem) that provides an executable. The chruby strategy does not have those issues so chruby is the recommended tool.

chnode installation:

brew tap tkareine/chnode
brew install tkareine/chnode/chnode
echo "source /usr/local/opt/chnode/share/chnode/chnode.sh" >>~/.zshrc
echo "source /usr/local/opt/chnode/share/chnode/auto.sh" >>~/.zshrc
echo "precmd_functions+=(chnode_auto)" >>~/.zshrc

chnode usage:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#    node-16.17.1
chnode 16.17.1
echo $PATH
# /Users/me/.nodes/node-16.17.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#  * node-16.17.1
echo 14.20.1 >.node-version
echo $PATH
# /Users/me/.nodes/node-14.20.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#  * node-14.20.1
#    node-16.17.1

chpython installation:

git clone https://github.com/kisoku/chpython.git
cd chpython
make install
echo "source /usr/local/share/chpython/chpython.sh" >>~/.zshrc
echo "source /usr/local/share/chpython/auto.sh" >>~/.zshrc

chpython usage:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#    python-3.9.13
chpython 3.9.13
echo $PATH
# /Users/me/.pythons/python-3.9.13/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#  * python-3.9.13
echo 3.8.10 >.python-version
echo $PATH
# /Users/me/.pythons/python-3.8.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#  * python-3.8.10
#    python-3.9.13
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文