linux下pip使用可能遇到的问题

今天想在实验楼上找项目练练手,为了偷懒,linux环境直接就用了win10内置的WSL(Windows内置的Ubuntu子系统,有着比虚拟机更高的系统权限,而且文件目录跟windows下一致)。在使用pip安装pillow库时出现了问题,解决后记录如下:


1、

lgc@Orankarl:~/.pip$ sudo pip install pillow
The directory '/home/lgc/.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.
The directory '/home/lgc/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pillow
python: ../sysdeps/posix/getaddrinfo.c:2591: getaddrinfo: Assertion `(__extension__ ({ const struct in6_addr *__a = (const struct in6_addr *) (sin6->sin6_addr.__in6_u.__u6_addr32); __a->__in6_u.__u6_addr32[0] == 0 && __a->__in6_u.__u6_addr32[1] == 0 && __a->__in6_u.__u6_addr32[2] == __bswap_32 (0xffff); }))' failed.

经过查资料得知,这种问题提示原因是在pip命令中使用了sudo,切换了用户权限,导致user非当前user的情况发生。将sudo去掉即可。要记住,所有pip命令都不要加sudo。


2、

lgc@Orankarl:/usr/local/lib/python2.7/dist-packages$ pip install pillow         
Collecting pillow
/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Using cached Pillow-4.2.1-cp27-cp27mu-manylinux1_x86_64.whl
Collecting olefile (from pillow)
  Using cached olefile-0.44.zip
Installing collected packages: olefile, pillow
  Running setup.py install for olefile ... done
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 377, in move_wheel_files
    clobber(source, dest, False, fixer=fixer, filter=filter)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/usr/local/bin/createfontdatachunk.py'


从最后一行可以看出,当前用户没有关于目录 /usr/local/bin/的权限,那么就给它权限好了,命令如下:

lgc@Orankarl:/usr/local/bin$ sudo chown -R lgc /usr/local/bin
其后如果还有类似错误也如法炮制即可,最终成功安装pillow库。


### 如何在 Linux使用 `pip` 安装 Python 包 #### 查看 Python 环境路径 为了确保安装包到正确的环境中,在安装之前应先确认 Python 解释器的位置。可以通过如下命令查找 Python 的安装路径: ```bash whereis python3 ``` 假设查询结果显示 `/home/user/miniconda3/bin/python` 是 Python 的路径,则后续操作需基于此路径。 #### 执行安装命令 对于大多数情况,推荐通过指定的 Python 可执行文件调用 pip 来安装所需的库,比如 Pandas 库: ```bash /home/user/miniconda3/bin/python -m pip install pandas[^1] ``` 如果遇到网络问题或其他原因导致无法正常连接官方 PyPI 仓库时,可尝试更换国内镜像源以加速下载过程并解决信任主机的问题。此时应该修改命令为: ```bash pip install --upgrade pip --trusted-host pypi.tuna.tsinghua.edu.cn /home/user/miniconda3/bin/python -m pip install pandas --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ ``` 以上方法适用于在线环境下的常规场景;而在完全隔离于互联网之外的离线服务器上部署应用时,则需要提前准备好所需依赖项,并将其上传至目标机器后再按指引完成配置与安装工作[^2]。 #### 注意事项 值得注意的是,当处于某个特定版本控制之下(例如 Anaconda 或者其他形式创建出来的虚拟环境),应当优先激活该环境再继续下一步骤的操作。这通常意味着要在终端里输入类似于下面这样的语句来切换当前使用的解释器及其关联资源集合: ```bash source activate your_env_name cd /path/to/package_directory ``` 最后提醒一点,移除不再需要的第三方扩展组件务必借助专门为此设计好的工具——即 pip 自身所提供的卸载功能,而不是简单粗暴地运用 rm 命令直接作用于磁盘上的文件夹结构之上,以免造成不必要的麻烦或破坏整个系统的稳定性[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值