How to install pip with Python 3?

后端 未结 21 1246
夕颜
夕颜 2020-11-22 01:06

I want to install pip. It should support Python 3, but it requires setuptools, which is available only for Python 2.

How can I install pip with Python 3?

21条回答
  •  迷失自我
    2020-11-22 02:06

    Assuming you are in a highly restricted computer env (such as myself) without root access or ability to install packages...

    I had never setup a fresh/standalone/raw/non-root instance of Python+virtualenv before this post. I had do quite a bit of Googling to make this work.

    1. Decide if you are using python (python2) or python3 and set your PATH correctly. (I am strictly a python3 user.) All commands below can substitute python3 for python if you are python2 user.
    2. wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-x.y.z.tar.gz
    3. tar -xzvf virtualenv-x.y.z.tar.gz
    4. python3 virtualenv-x.y.z/virtualenv.py --python $(which python3) /path/to/new/virtualenv
    5. source /path/to/new/virtualenv/bin/activate
      • Assumes you are using a Bourne-compatible shell, e.g., bash
      • Brilliantly, this virtualenv package includes a standalone version of pip and setuptools that are auto-magically installed into each new virtualenv. This solves the chicken and egg problem.
      • You may want to create an alias (or update your ~/.bashrc, etc.) for this final command to activate the python virtualenv during each login. It can be a pain to remember all these paths and commands.
    6. Check your version of python now: which python3 should give: /path/to/new/virtualenv/bin/python3
    7. Check pip is also available in the virtualenv via which pip... should give: /path/to/new/virtualenv/bin/pip

    Then... pip, pip, pip!

    Final tip to newbie Pythoneers: You don't think you need virtualenv when you start, but you will be happy to have it later. Helps with "what if" installation / upgrade scenarios for open source / shared packages.

    Ref: https://virtualenv.pypa.io/en/latest/installation.html

提交回复
热议问题