Changing default python to another version

后端 未结 7 663
死守一世寂寞
死守一世寂寞 2021-01-01 05:35

Currently when I use \"python\" command, it points to python2.6. I have installed python3.1 and I want the \"python\" command point to python3.1. How it is possible?

相关标签:
7条回答
  • 2021-01-01 06:05

    You could follow this procedure:

    sudo rm /usr/bin/python

    sudo ln -s /usr/bin/python3.1 /usr/bin/python

    But as already stated by Petr Viktorin, any programs that would expect python v2 would stop to work. So use with caution. You can undo the change by running:

    sudo rm /usr/bin/python

    sudo ln -s /usr/bin/python2.6 /usr/bin/python

    0 讨论(0)
  • 2021-01-01 06:12

    On Linux/Mac OS you can use python3 instead of python.

    0 讨论(0)
  • 2021-01-01 06:19
    unlink /usr/bin/python
    ln -s /usr/bin/python3.1 /usr/bin/python
    
    0 讨论(0)
  • 2021-01-01 06:22

    Since you have Linux, and if you want to simply type "python" instead of "python3" in order to run Python programs, a solution is simply to define an alias in you shell configuration file (.bashrc, etc.). For Bourne shells, it should be something like

    alias python=python3
    

    (or whatever your Python 3 name is).

    This way, you do not have to change anything on your system, so this solution should quite innocuous and it should not break your system.

    0 讨论(0)
  • 2021-01-01 06:22

    You really don't want to change what python points to, because some programs might expect Python 2, and break.

    The solution is to use virtualenv: create an isolated Python 3 environment (with the -p python3 option), activate it, and you're good to go.

    0 讨论(0)
  • 2021-01-01 06:27

    It is not advisable.

    You could write at the top in your own script (a shebang):

    #!/usr/bin/env python3
    

    If you're on Windows then install pylauncher. It understands #!.

    On Linux to make your script executable, run once:

    $ chmod +x your-script
    

    After that, to run your script:

    $ ./your-script
    

    For interactive use you could create virtualenv as @Petr Viktorin points out. To install/upgrade (versions from Ubuntu's repositries are too old):

    $ pip install -U virtualenv{,wrapper}
    

    Follow instructions in /path/to/virtualenvwrapper.sh, to create virtualenv that uses python3:

    $ mkvirtualenv --python python3 py3
    

    To activate virtualenv:

    $ workon py3
    

    In an active virtualenv python refers to /path/virtualenv/bin/python. So you could run:

    $ python your_module.py
    
    0 讨论(0)
提交回复
热议问题