无法使用 pip(ubuntu)

无法使用 pip(ubuntu)
$ sudo pip install numpy    # or anything else

错误:

The directory '/home/user/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. (tried sudo -H, the rest errors persist)  
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.    
Collecting numpy  
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/  
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.  
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping  
$ python -V
Python 3.7.3  
$ pip -V
pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
$ whereis pip
pip: /usr/local/bin/pip2.7 /usr/local/bin/pip3.7 /usr/local/bin/pip /usr/local/bin/pip3.6  

pip3.6 出现后:

$ sudo su
$ update-alternatives --install /usr/bin/python python /usr/bin/python3 1

在 anaconda 环境中,pip 运行良好。

在不破坏系统的情况下,安全地删除与 python 相关的内容,然后正确地重新安装内容,这是一个更好的解决方案吗?该怎么做呢?

我知道还有很多类似的问题,但是我已经尝试了很多方法(显然,我搞砸了一些东西)但还是没有解决问题。

谢谢!

答案1

sudo1- 避免在不需要时使用。2-
由于您使用的是 Python3,因此在 Python3 中安装包的适当方法是使用 pip3。
因此命令如下:pip3 install --user <package_name> 其中:

  • pip3适用于 Python3。

  • install使用 pip3 安装包。

  • --user将下载的包保存在当前用户目录中(这样您不需要更多权限)。

  • package_name任何 Python 包。

编辑:
1.安装Python和ssl必要的包:sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

  1. 从以下网址下载并解压“Python-3.7.0.tar.xz”https://www.python.org/ftp/python/进入你的主目录。

  2. 在该目录中打开终端并运行:./configure

  3. 构建并安装:sudo make && sudo make install

  4. 使用以下方法安装软件包:pip3 install package_name

相关内容