Changing default python to another version

后端 未结 7 758
死守一世寂寞
死守一世寂寞 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: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
    

提交回复
热议问题