How to update-alternatives to Python 3 without breaking apt?

前端 未结 4 482
忘掉有多难
忘掉有多难 2020-12-01 03:01

The other day I decided that I wanted the command python to default to firing up python3 instead of python2.

So I did this:

$ sudo update-alternative         


        
4条回答
  •  一个人的身影
    2020-12-01 03:29

    Per Debian policy, python refers to Python 2 and python3 refers to Python 3. Don't try to change this system-wide or you are in for the sort of trouble you already discovered.

    Virtual environments allow you to run an isolated Python installation with whatever version of Python and whatever libraries you need without messing with the system Python install.

    With recent Python 3, venv is part of the standard library; with older versions, you might need to install python3-venv or a similar package.

    $HOME~$ python --version
    Python 2.7.11
    
    $HOME~$ python3 -m venv myenv
    ... stuff happens ...
    
    $HOME~$ . ./myenv/bin/activate
    
    (myenv) $HOME~$ type python   # "type" is preferred over which; see POSIX
    python is /home/you/myenv/bin/python
    
    (myenv) $HOME~$ python --version
    Python 3.5.1
    

    A common practice is to have a separate environment for each project you work on, anyway; but if you want this to look like it's effectively system-wide for your own login, you could add the activation stanza to your .profile or similar.

提交回复
热议问题