How to override the pip command to Python3.x instead of Python2.7?

后端 未结 7 1183
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 20:07

I am using OSX and I have pip installed for both Python3.5 and Python2.7. I know I can run the command pip2 to use Python2 and when I use the command pip3

7条回答
  •  再見小時候
    2020-12-02 20:31

    For your projects, you should be using a virtualenv.

    You can choose which python will be that of the virtualenv at creation time, by specifying it on the command line:

    virtualenv -p python3 env
    # then
    . env/bin/activate
    python              # ← will run python3
    

    That python interpreter will be the one used when you run python or pip while the virtualenv is active.

    Under the hood, activating the virtualenv will:

    • modify your PATH environment setting so binaries in env/bin override those from your system.
    • modify your PYTHONHOME environment setting so python modules are loaded from env/lib.

    So python, pip and any other package you install with pip will be run from the virtualenv, with the python version you chose and the package versions you installed in the virtualenv.

    Other than this, running python without using virtualenv will just run the default python of the system, which you cannot usually change as it would break a lot of system scripts.

提交回复
热议问题