python多版本管理

作者: PyChina | 来源:发表于2016-09-06 20:34 被阅读63437次

    作者:oliverhuang 来源:简书
    mac不要轻易的安装新版本的python!
    mac不要轻易的安装新版本的python!
    mac不要轻易的安装新版本的python!

    不同的版本用python的版本管理工具
    一般重要的事情说三遍,因为用到一个python的新功能,所以需要安装。2.7.9以上的版本,但是mac上面是2.7.6的,如果真的想要更新,下载官方的pkg并且设置一下环境变量,我就是没有设置环境变量导致了一系列的问题。配置方法找到一个博客
    担心以后博客消失,摘录一下,作为参考

    Installing / Updating Python on OS X

    While Python comes pre-installed on OS X, Apple doesn’t do a good job on keeping the Python runtime environment up to date. Currently, on Mac OS X 10.7.4 “Lion”, entering python -V returns Python 2.7.1
    . Even worse, Mac OS X 10.6 “Snow Leopard” is still on a Python 2.6 release.

    While the latest Python releases are always available on http://www.python.org, updating a Mac isn’t a simple, straight forward process.
    Follow along and update your Mac to Python 2.7.3, or 3.3.0 or whatever the newest 2.x and 3.x release might be, when you read this. To update your Mac to something like Python 2.7.3, I assume that

    • your Mac-User account is setup as an “Administrator” account.
    • your Mac already has this folder: /System/Library/Frameworks/Python.framework/Versions/

    To read about how to upgrade to Python 3.3, jump to the very bottom of this post.

    1. Downloading and Installing the latest Python Release

    Go to python.org and pick the most recent Python 2.x release: http://python.org/download.I picked Python 2.7.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later), an about 18 MB download.
    Opening the DMG and executing the installer (Python.mpkg) will install the Python release into /Library/Frameworks/Python.framework, which is not next to the other, already installed Python versions and may lead to some nasty incompatibilities. Moreover,/usr/bin/python
    still executes python 2.7.1., i.e. some work still needs to be done to really update Python on your Mac.

    2. Moving Python into the right place

    If Python 2.7.x is already available on the Mac (e.g. on OS X Lion), open a terminal and enter:
    <pre><code> sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7</pre></code>

    This will delete Python 2.7.

    In any case, now enter this into the terminal
    <pre><code>sudo mv /Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions

    </pre></code>

    3. Fixing the Group

    Setting group to wheel, just like it’s done for the already installed Python Versions:
    <pre><code>sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/2.7
    </pre></code>

    4. Updating the Current Link

    <pre><code>sudo rm /System/Library/Frameworks/Python.framework/Versions/Current

    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current</pre></code>

    5. Fixing /usr/bin

    Since python, pythonw, etc. are not linked but actually reside in the/usr/bin
    directory,/usr/bin/python
    still executes python 2.7.1.Therefore, we are going to remove the old python executable files and replacing them with links into /System/Library/Frameworks/Python.framework/Versions/Current:

    5.1. Removing old copies

    <pre><code>sudo rm /usr/bin/pydoc

    sudo rm /usr/bin/python

    sudo rm /usr/bin/pythonw

    sudo rm /usr/bin/python-config</pre></code>

    5.2. Creating links into /System/…/Python.framework/Versions/Current

    <pre><code>sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc /usr/bin/pydoc

    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python

    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw /usr/bin/python

    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config /usr/bin/python-config</pre></code>

    6. Updating .bash_profile

    Use TextMate, pico, or your favorite text editor to edit the hidden ~/.bash_profile file. If you want to be able to execute python tools like idle easily, without providing the path, edit the PATH for Python like this:

    <pre>code>python # Setting PATH for Python 2.7
    # The orginal version is saved in .bash_profile.pysave
    PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}”

    export PATH</pre></code>

    However, you can also remove those four lines altogether or fall-back to the .bash_profile.pysave file.
    Try it by closing the terminal app, re-opening it, and entering python -Vas well as /usr/bin/python -V.

    7. Updating your IDE

    The last remaining step it to tell your IDE about the new Python release that you just installed.
    I hope you have discovered PyCharm, JetBrain’s IDE for Python and Django.In PyCharm, you would open its properties and under “Project Interpreters” change the Python Interpreter, by pointing it to the/System/Library/Frameworks/Python.framework/Versions/2.7
    directory.

    python-pycharm

    Updating to Python 3.3

    Following the same procedure, you can also update to Python 3.3. E.g, I downloaded the Python 3.3.0 Mac OS X 64-bit/32-bit x86-64/i386 Installer form here: http://python.org/download/
    After running the included installer, a script like this (run with sudo) should put everything into the right place and make python 3.3 the default python version.

    <pre><code>#!/bin/bash

    rm -R /System/Library/Frameworks/Python.framework/Versions/3.3

    mv /Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions
    chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.3

    rm /System/Library/Frameworks/Python.framework/Versions/Current

    ln -s /System/Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions/Current

    rm /usr/bin/pydoc

    rm /usr/bin/python

    rm /usr/bin/pythonw

    rm /usr/bin/python-config

    rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc

    rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python

    rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw

    rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python-config

    ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc3 /usr/bin/pydoc

    ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /usr/bin/python

    ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3 /usr/bin/pythonw

    ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3-config /usr/bin/python-config</pre></code>

    如果安装完了,但是没有配置环境变量的话,可以直接卸载python

    <pre><code>cd /usr/local/bin;

    ls -l . | grep '../Library/Frameworks/Python.framework/Versions/2.7' | awk '{print $9}' | xargs rm</pre></code>

    所以还是使用类似nvm的管理node的工具比如pyenv或者pythonbrew
    pythonbrew开发者都说了

    This project is no longer under active development.
    You are encouraged to try out pyenv instead.

    那就介绍一下 pyenv
    用神器homebrew安装一下,如果出现Warning: The post-install step did not complete successfully
    可以使用一下sudo chown -R
    whoami/usr/local/lib/node_modules

    安装完毕后的命令在这里
    这里也粘贴一份作为存档,但是还是以官方为准。

    Command Reference
    Likegit
    , thepyenv
    command delegates to subcommands based on itsfirst argument.
    The most common subcommands are:
    pyenv commands

    pyenv local

    pyenv global

    pyenv shell

    pyenv install

    pyenv uninstall

    pyenv rehash

    pyenv version

    pyenv versions

    pyenv which

    pyenv whence

    pyenv commands

    Lists all available pyenv commands.
    pyenv local

    Sets a local application-specific Python version by writing the versionname to a.python-version
    file in the current directory. This versionoverrides the global version, and can be overridden itself by settingthePYENV_VERSION
    environment variable or with thepyenv shell
    command.

    <pre><code>$ pyenv local 2.7.6</pre></code>

    When run without a version number, pyenv local reports the currently
    configured local version. You can also unset the local version:

    <pre><code>$ pyenv local --unset</pre></code>

    Previous versions of pyenv stored local version specifications in a
    file named .pyenv-version. For backwards compatibility, pyenv will
    read a local version specified in an .pyenv-version file, but a
    .python-version file in the same directory will take precedence.

    peen local (advanced)
    You can specify multiple versions as local Python at once.

    Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

    <pre><code>$ pyenv local 2.7.6 3.3.3

    $ pyenv versions
    system

    • 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
    • 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)

    $ python --version

    Python 2.7.6

    $ python2.7 --version

    Python 2.7.6

    $ python3.3 --version

    Python 3.3.3</pre></code>

    pyenv global
    Sets the global version of Python to be used in all shells by writing
    the version name to the ~/.pyenv/version file. This version can be
    overridden by an application-specific .python-version file, or by
    setting the PYENV_VERSION environment variable.

    <pre><code>$ pyenv global 2.7.6</pre></code>

    The special version name system tells pyenv to use the system Python
    (detected by searching your $PATH).

    When run without a version number, pyenv global reports the
    currently configured global version.

    pyenv global (advanced)
    You can specify multiple versions as global Python at once.

    Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

    <pre><code>$ pyenv global 2.7.6 3.3.3
    $ pyenv versions
    system

    • 2.7.6 (set by /Users/yyuu/.pyenv/version)

    • 3.3.3 (set by /Users/yyuu/.pyenv/version)

    $ python --version

    Python 2.7.6

    $ python2.7 --version

    Python 2.7.6

    $ python3.3 --version

    Python 3.3.3</pre></code>

    pyenv shell
    Sets a shell-specific Python version by setting the PYENV_VERSION
    environment variable in your shell. This version overrides
    application-specific versions and the global version.

    <pre><code>$ pyenv shell pypy-2.2.1</pre></code>
    When run without a version number, pyenv shell reports the current
    value of PYENV_VERSION. You can also unset the shell version:
    <pre><code>$ pyenv shell --unset</pre></code>

    Note that you'll need pyenv's shell integration enabled (step 3 of
    the installation instructions) in order to use this command. If you
    prefer not to use shell integration, you may simply set the
    PYENV_VERSION variable yourself:

    <pre><code>$ export PYENV_VERSION=pypy-2.2.1</pre></code>
    pyenv shell (advanced)
    You can specify multiple versions via PYENV_VERSION at once.

    Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

    <pre><code>
    $ pyenv shell 2.7.6 3.3.3

    $ pyenv versions
    system

    • 2.7.6 (set by PYENV_VERSION environment variable)
    • 3.3.3 (set by PYENV_VERSION environment variable)

    $ python --version

    Python 2.7.6

    $ python2.7 --version

    Python 2.7.6

    $ python3.3 --version

    Python 3.3.3</pre></code>

    or, if you prefer 3.3.3 over 2.7.6,
    <pre><code>
    $ pyenv shell 3.3.3 2.7.6

    $ pyenv versions
    system

    • 2.7.6 (set by PYENV_VERSION environment variable)
    • 3.3.3 (set by PYENV_VERSION environment variable)
      venv27

    $ python --version

    Python 3.3.3

    $ python2.7 --version

    Python 2.7.6

    $ python3.3 --version

    Python 3.3.3</pre></code>
    pyenv install
    Install a Python version (using python-build).
    <pre><code>Usage: pyenv install [-f] [-kvp] <version>

           pyenv install [-f] [-kvp] <definition-file> 
    
           pyenv install 
    

    -l|--list -l/--list List all available versions

    -f/--force Install even if the version appears to be installed already

    python-build options:

    -k/--keep Keep source tree in

    $PYENV_BUILD_ROOT after installation (defaults to

    $PYENV_ROOT/sources)

    -v/--verbose Verbose mode: print compilation status to stdout -p/--patch Apply a patch from stdin before building

    -g/--debug Build a debug version

    </pre></code>
    pyenv uninstall
    Uninstall a specific Python version.
    <pre><code>Usage: pyenv uninstall [-f|--force] <version> -f Attempt to remove the specified version without prompting for confirmation. If the version does not exist, do not display an error message.

    </pre></code>

    pyenv rehash
    Installs shims for all Python binaries known to pyenv (i.e.,
    ~/.pyenv/versions//bin/). Run this command after you install a new
    version of Python, or install a package that provides binaries.

    <pre><code>$ pyenv rehash</pre></code>

    pyenv version
    Displays the currently active Python version, along with information on
    how it was set.

    <pre><code>$ pyenv version
    2.7.6 (set by /home/yyuu/.pyenv/version)
    </pre></code>

    pyenv versions
    Lists all Python versions known to pyenv, and shows an asterisk next to
    the currently active version.

    <pre><code>$ pyenv versions

    2.5.6

    2.6.8

    • *2.7.6 (set by /home/yyuu/.pyenv/version)
      3.3.3
      jython-2.5.3
      pypy-2.2.1</pre></code>

    pyenv which
    Displays the full path to the executable that pyenv will invoke when
    you run the given command.

    <pre><code>$ pyenv which python3.3
    /home/yyuu/.pyenv/versions/3.3.3/bin/python3.3</pre></code>

    pyenv whence
    Lists all Python versions with the given command installed.

    <pre><code>$ pyenv whence 2to3

    2.6.8

    2.7.6

    3.3.3</pre></code>
    pyenv install
    Part of Python-build, this installs versions of python
    <pre><code>$ pyenv install 2.7.6

    $ pyenv install 2.6.8

    $ pyenv versions
    system

    2.6.8

    • 2.7.6 (set by /home/yyuu/.pyenv/version)</pre></code>
      pyenv install --list
      List available remote versions of Python, including Anaconda, Jython, pypy, and stackless
      <pre><code>$ pyenv install --list</pre></code>

    开源编程语言Python近年来在互联网、游戏、云计算、大数据、运维、企业软件等领域有非常多的应用。

    PyCon大会是Python语言社群全球性的盛会,PyConChina 是由 CPyUG(华蠎用户组)获得授权举办的中国PyCon年会。过去5年在北京、上海、广州、珠海、杭州、苏州、西安、合肥等地举办过十几次大会。

    今年第六届大会PyConChina2016,由PyChina.org发起,CPyUG/TopGeek 等社区协办,将在2016年9月10日(上海)9月23日(深圳)10月15日(北京)地举办的针对Python开发者所举办的最盛大和权威的Python相关技术会议,由PyChina社区主办,致力于推动各类Python相关的技术在互联网、企业应用等领域的研发和应用。

    您可以点击此处了解更多详情,或者扫描下图二维码:

    相关文章

      网友评论

        本文标题:python多版本管理

        本文链接:https://www.haomeiwen.com/subject/rhodettx.html