简体   繁体   English

如何在 Ubuntu 18 上为 python 3.7 安装 pip?

[英]How to Install pip for python 3.7 on Ubuntu 18?

EDIT 18/02: Since I still don't have a solution, I'm updating with what I know so far.编辑 18/02:由于我仍然没有解决方案,我正在更新我目前所知道的。

I've installed python 3.7 successfully.我已经成功安装了 python 3.7。 I can install modules using pip (or pip3) but those modules are installed in Python 3.6 (Comes with ubuntu).我可以使用 pip(或 pip3)安装模块,但这些模块安装在 Python 3.6(随 ubuntu 一起提供)中。 Therefore I can't import those modules in python 3.7 (get a module not found) Python 3.7 doesn't recognize pip/pip3, so I can't install through pip/pip3 I need python 3.7因此我无法在 python 3.7 中导入这些模块(找不到模块)Python 3.7 无法识别 pip/pip3,所以我无法通过 pip/pip3 安装我需要 python 3.7

-- ——

I've installed Python 3.7 on my Ubuntu 18.04 machine.我已经在我的 Ubuntu 18.04 机器上安装了 Python 3.7。 Following this instructions in case it's relevant:如果相关,请按照此说明进行操作:

Download : Python 3.7 from Python Website [1] ,on Desktop and manually unzip it, on Desktop Installation : Open Terminal (ctrl +shift+T)从 Python 网站 [1] 下载:Python 3.7,在桌面上并手动解压缩,在桌面安装:打开终端 (ctrl +shift+T)

 Go to the Extracted folder $ cd ~/Desktop/Python-3.7.0 $ ./configure $ make $ sudo make install

Making Python 3.7 default Python :使 Python 3.7 默认 Python :

 $ sudo vim ~/.bashrc press i on the last and new line - Type alias python= python3.7 press Esc type - to save and exit vim :wq now type $ source ~/.bashrc

From here: https://www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04从这里: https : //www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04

I've downloaded several modules through pip install module but when I try to import them, I get a ModuleNotFoundError: No module names 'xx'我已经通过pip install module下载了几个模块,但是当我尝试导入它们时,出现ModuleNotFoundError: No module names 'xx'

So I did some research and apparently when used pip to install, it installed in the modules in previous version of Python.所以我做了一些研究,显然当使用 pip 安装时,它安装在以前版本的 Python 的模块中。 Somewhere (probably a question in SO) I found a suggestion to install the module using python3.7 -m pip install module but then I get /usr/local/bin/python3.7: no module named pip .在某处(可能是 SO 中的一个问题)我发现了一个使用python3.7 -m pip install module的建议,但后来我得到了/usr/local/bin/python3.7: no module named pip

Now I'm stuck, pip is installed, but apparently not for Python 3.7.现在我被卡住了,pip 已安装,但显然不适用于 Python 3.7。 I'm assuming that if I can install pip for Python 3.7, I can run the pip install command and get the modules I need.我假设如果我可以为 Python 3.7 安装 pip,我可以运行 pip install 命令并获取我需要的模块。 If that is the case, how can I install pip for python 3.7, since it's already installed?如果是这种情况,我如何为 python 3.7 安装 pip,因为它已经安装了?

In general, don't do this :一般来说,不要这样做

pip install package

because, as you have correctly noticed, it's not clear what Python version you're installing package for.因为,正如您已经正确注意到的,不清楚您要为哪个 Python 版本安装package

Instead, if you want to install package for Python 3.7, do this :相反,如果要为 Python 3.7 安装package请执行以下操作

python3.7 -m pip install package

Replace package with the name of whatever you're trying to install.用您尝试安装的任何名称替换package

Took me a surprisingly long time to figure it out, too.我也花了很长时间才弄明白。 The docs about it are here .关于它的文档在这里

Your other option is to set up a virtual environment .您的另一个选择是设置一个虚拟环境 Once your virtual environment is active, executable names like python and pip will point to the correct ones.一旦您的虚拟环境处于活动状态, pythonpip等可执行名称将指向正确的名称。

A quick add-on to mpenkov's answer above (didn't want this to get lost in the comments)上面 mpenkov 的回答的快速补充(不想在评论中迷失)

For me, I had to install pip for 3.6 first对我来说,我必须先为 3.6 安装 pip

sudo apt install python3-pip

now you can install python 3.7现在你可以安装 python 3.7

sudo apt install python3.7

and then I could install pip for 3.7然后我可以为 3.7 安装 pip

python3.7 -m pip install pip

and as a bonus, to install other modules just preface with作为奖励,安装其他模块只是在前言

python3.7 -m pip install <module>

EDIT 1 (12/2019):编辑 1 (12/2019):

I know this is obvious for most.我知道这对大多数人来说是显而易见的。 but if you want python 3.8, just substitute python3.8 in place of python3.7但是如果你想要 python 3.8,只需用python3.8代替python3.7

EDIT 2 (5/2020):编辑 2 (5/2020):

For those that are able to upgrade, Python 3.8 is available out-of-the-box for Ubuntu 20.04 which was released a few weeks ago.对于那些能够升级的用户,几周前发布的 Ubuntu 20.04 可以直接使用 Python 3.8。

This works for me.这对我有用。

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then this command with sudo:然后使用 sudo 执行此命令:

python3.7 get-pip.py

Based on this instruction .基于此指令

I used apt-get to install python3.7 in ubuntu18.04.我在ubuntu18.04中使用apt-get安装python3.7。 The installations are as follows.安装如下。

  1. install python3.7安装python3.7
sudo apt-get install python3.7 
  1. install pip3.安装pip3。 It should be noted that this may install pip3 for python3.6.需要注意的是,这可能会为python3.6安装pip3。
sudo apt-get install python3-pip 
  1. change the default of python3 for python3.7.为 python3.7 更改 python3 的默认值。 This is where the magic is, which will make the pip3 refer to python3.7.这就是魔法所在,这将使 pip3 引用 python3.7。
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

Hope it works for you.希望对你有效。

To install all currently supported python versions (python 3.6 is already pre-installed) including pip for Ubuntu 18.04 do the following:要安装所有当前支持的 python 版本(python 3.6 已经预安装),包括 Ubuntu 18.04 的 pip,请执行以下操作:

To install python3.5 and python3.7, use the deadsnakes ppa:要安装 python3.5 和 python3.7,请使用 deadsnakes ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5
sudo apt-get install python3.7

Install python2.7 via distribution packages:通过分发包安装python2.7:

sudo apt install python-minimal  # on Ubuntu 18.04 python-minimal maps to python2.7

To install pip use:要安装 pip 使用:

sudo apt install python-pip  # on Ubuntu 18.04 this refers to pip for python2.7
sudo apt install python3-pip  # on Ubuntu 18.04 this refers to pip for python3.6
python3.5 -m pip install pip # this will install pip only for the current user
python3.7 -m pip install pip

I used it for setting up a CI-chain for a python project with tox and Jenkins.我用它为带有 tox 和 Jenkins 的 python 项目设置 CI 链。

Combining the answers from @mpenkon and @dangel, this is what worked for me:结合@mpenkon 和@dangel 的答案,对我有用:

  1. sudo apt install python3-pip

  2. python3.7 -m pip install pip

Step #1 is required (assuming you don't already have pip for python3) for step #2 to work.第 1 步是必需的(假设您还没有用于 python3 的 pip)才能使第 2 步工作。 It uses pip for Python3.6 to install pip for Python 3.7 apparently.它显然使用 Python3.6 的 pip 来安装 Python 3.7 的 pip。

The following steps can be used:可以使用以下步骤:


sudo apt-get -y update
---------
sudo apt-get install python3.7
--------------
 python3.7
-------------
 curl -O https://bootstrap.pypa.io/get-pip.py
-----------------
sudo apt install python3-pip
-----------------
sudo apt install python3.7-venv
-----------------
 python3.7 -m venv /home/ubuntu/app
-------------
 cd app   
----------------
 source bin/activate

When i use apt install python3-pip , i get a lot of packages need install, but i donot need them.当我使用apt install python3-pip ,我得到了很多需要安装的包,但我不需要它们。 So, i DO like this:所以,我喜欢这样:

apt update
apt-get install python3-setuptools
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
rm -f get-pip.py

For those who intend to use venv :对于那些打算使用venv

If you don't already have pip for Python 3:如果您还没有 Python 3 的pip

sudo apt install python3-pip

Install venv package:安装venv包:

sudo apt install python3.7-venv

Create virtual environment (which will be bootstrapped with pip by default):创建虚拟环境(默认使用pip引导):

python3.7 -m venv /path/to/new/virtual/environment

To activate the virtual environment, source the appropriate script for the current shell, from the bin directory of the virtual environment.要激活虚拟环境, source相应的脚本为当前shell,从bin虚拟环境的目录。 The appropriate scripts for the different shells are:不同 shell 的适当脚本是:

bash/zsh – activate bash/zsh – activate

fish – activate.fish鱼 - activate.fish

csh/tcsh – activate.csh csh/tcsh – activate.csh

For example, if using bash:例如,如果使用 bash:

source /path/to/new/virtual/environment/bin/activate

Optionally, to update pip for the virtual environment (while it is activated):可选地,更新虚拟环境的pip (当它被激活时):

pip install --upgrade pip

When you want to deactivate the virtual environment:当您要停用虚拟环境时:

deactivate 

I installed pip3 using我安装了pip3使用

python3.7 -m pip install pip

But upon using pip3 to install other dependencies, it was using python3.6.但是在使用pip3安装其他依赖项时,它使用的是 python3.6。
You can check the by typing pip3 --version您可以通过输入pip3 --version来检查

Hence, I used pip3 like this (stated in one of the above answers):因此,我像这样使用了pip3 (在上述答案之一中说明):

python3.7 -m pip install <module>

or use it like this:或者像这样使用它:

python3.7 -m pip install -r requirements.txt

I made a bash alias for later use in ~/.bashrc file as alias pip3='python3.7 -m pip' .我在 ~/.bashrc 文件中alias pip3='python3.7 -m pip'了一个 bash 别名作为alias pip3='python3.7 -m pip' If you use alias, don't forget to source ~/.bashrc after making the changes and saving it.如果您使用别名,请不要忘记在进行更改并保存后source ~/.bashrc

Install python pre-requisites安装python先决条件

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Install python 3.7 (from ppa repository)安装 python 3.7(来自 ppa 存储库)

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7

Install pip3.7安装pip3.7

sudo apt install python3-pip
python3.7 -m pip install pip

Create python and pip alternatives创建 python 和 pip 替代品

sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.7 10
sudo update-alternatives --install /usr/local/bin/pip pip /home/your_username/.local/bin/pip3.7 10

Make changes做出改变

source ~/.bashrc
python --version
pip --version

How about simply简单的怎么样

add-apt-repository ppa:deadsnakes/ppa
apt-get update
apt-get install python3.7-dev
alias pip3.7="python3.7 -m pip"

Now you have the command现在你有了命令

pip3.7

separately from pip3.与 pip3 分开。

curl https://bootstrap.pypa.io/get-pip.py | sudo python3.7

如果一切都失败了。

pip3 not pip . pip3不是pip You can create an alias like you did with python3 if you like.如果愿意,您可以像使用python3一样创建别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM