pip软件包简介

一、Pip 简介

  通常 Linux 系统管理员有两种方式来安装一个 Python 的软件包:一种是通过系统的包管理工具(如 apt-get)从系统的软件仓库里安装,一种是通过Python自己的包管理工具(如 easy_install或者pip)从Python Cheese Shop中下载安装。笔者推荐使用pip。pip是一个可以代替easy_install的安装和管理Python软件包的工具,是一个安装Python库很方便的工具,功能类似YUM。pip可以使用install语句安装模块,可以升级模块,并且可以删除你之前所安装的模块,但是easy_install不可以卸载之前使用easy_install安装的模块。在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要我们手动安装。Python2.7.9及后续版本, Python3.4及后续版本已经默认安装了pip,所以推荐使用最新版本的Python(Python2或者Python3)就不需要再安装pip了。注意CentOS和Fedora下安装Python-pip后,关键字不是pip而是pip-Python。

Debian 和 Ubuntu:
$ sudo apt-get install python-pip
Centos 和 RedHat、Fedora:

  按理说是不是直接 yum install python-pip 就 OK 了啊,但是,却报错了

[root@localhost ~]# yum install python-pip
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
No package python-pip available.
Error: Nothing to do
网上说这种情况得首先安装epel扩展源:yum -y install epel-release,可是我还是报错

[root@localhost ~]# yum -y install epel-release
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
No package epel-release available.
Error: Nothing to do

  后来我思考了下,可能是本地源的问题,后来发现还真是这个原因(有的时候服务器需要下载一些软件,是本地 yum 源没有的,这时候需要配置网络 yum 源)

设置网络源:

来自:配置网络yum源

#cd  /etc/yum.repos.d/
把这个目录下的配置文件备份,改名;或者是把文件里边的enabled=1的改为enabled=0# wget -O /etc/yum.repos.d/CentOS-Base.repo   http://mirrors.aliyun.com/repo/Centos-6.repo
# sed -i  's/$releasever/6/g' /etc/yum.repos.d/CentOS-Base.repo
# yum clean all
# yum list
以上是配置RHEL6.5的,下边是配置 RHEL7.2
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# sed -i  's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
# yum clean all
# yum list
然后再执行yum -y install epel-release和yum -y install python-pip就OK了

二、常用命令

首先卸载numpy1.19.4:
pip uninstall numpy

指定版本安装:
pip install numpy==1.19.3

三、pip 安装遇到的问题

1. pip3 装 sqlalchemy 时报错
[root@heheda xiaoqiang]# pip3 install sqlalchemy
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting sqlalchemy
  Downloading https://files.pythonhosted.org/packages/e4/d2/b69f95ed3adb43f74afc974b159b5439ca8e59608354b5ad918bdd2af486/SQLAlchemy-1.4.50-cp36-cp36m-manylinux_2_5_x86_64.
manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6MB)    100% |████████████████████████████████| 1.6MB 416kB/s 
Collecting greenlet!=0.4.17; python_version >= "3" and (platform_machine == "aarch64" or (platform_machine == "ppc64le" or (platform_machine == "x86_64" or (platform_machi
ne == "amd64" or (platform_machine == "AMD64" or (platform_machine == "win32" or platform_machine == "WIN32")))))) (from sqlalchemy)  Downloading https://files.pythonhosted.org/packages/1e/1e/632e55a04d732c8184201238d911207682b119c35cecbb9a573a6c566731/greenlet-2.0.2.tar.gz (164kB)
    100% |████████████████████████████████| 174kB 861kB/s 
Collecting importlib-metadata; python_version < "3.8" (from sqlalchemy)
  Downloading https://files.pythonhosted.org/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->sqlalchemy)
  Downloading https://files.pythonhosted.org/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata; python_version < "3.8"->sqlalchemy)
  Downloading https://files.pythonhosted.org/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Installing collected packages: greenlet, zipp, typing-extensions, importlib-metadata, sqlalchemy
  Running setup.py install for greenlet ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ebnisytk/greenlet/setup.py';f=getattr(tokenize, 'open', open)
(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-2__fy6n_-record/install-record.txt --single-version-externally-managed --compile:    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/__init__.py -> build/lib.linux-x86_64-3.6/greenlet
    creating build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/platform
    creating build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/leakcheck.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_contextvars.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_cpp.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_extension_interface.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_gc.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_generator.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_generator_nested.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_greenlet.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_greenlet_trash.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_leaks.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_stack_saved.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_throw.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_tracing.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_version.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/test_weakref.py -> build/lib.linux-x86_64-3.6/greenlet/tests
    running egg_info
    writing src/greenlet.egg-info/PKG-INFO
    writing dependency_links to src/greenlet.egg-info/dependency_links.txt
    writing requirements to src/greenlet.egg-info/requires.txt
    writing top-level names to src/greenlet.egg-info/top_level.txt
    reading manifest file 'src/greenlet.egg-info/SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no previously-included files found matching 'benchmarks/*.json'
    no previously-included directories found matching 'docs/_build'
    warning: no files found matching '*.py' under directory 'appveyor'
    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    warning: no previously-included files matching '.coverage' found anywhere in distribution
    writing manifest file 'src/greenlet.egg-info/SOURCES.txt'
    copying src/greenlet/greenlet.cpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet.h -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_allocator.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_compiler_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_cpython_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_exceptions.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_greenlet.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_internal.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_refs.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_slp_switch.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_thread_state.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_thread_state_dict_cleanup.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/greenlet_thread_support.hpp -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/slp_platformselect.h -> build/lib.linux-x86_64-3.6/greenlet
    copying src/greenlet/platform/setup_switch_x64_masm.cmd -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_aarch64_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_alpha_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_amd64_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_arm32_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_arm32_ios.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_arm64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_arm64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_arm64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_csky_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_m68k_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_mips_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc64_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc64_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc_macosx.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_ppc_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_riscv_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_s390_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_sparc_sun_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x32_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x86_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/platform/switch_x86_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform
    copying src/greenlet/tests/_test_extension.c -> build/lib.linux-x86_64-3.6/greenlet/tests
    copying src/greenlet/tests/_test_extension_cpp.cpp -> build/lib.linux-x86_64-3.6/greenlet/tests
    running build_ext
    building 'greenlet._greenlet' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/src
    creating build/temp.linux-x86_64-3.6/src/greenlet
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -gre
cord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c src/greenlet/greenlet.cpp -o build/temp.linux-x86_64-3.6/src/greenlet/greenlet.o    src/greenlet/greenlet.cpp:16:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    
    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ebnisytk/greenlet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read()
.replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-2__fy6n_-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-ebnisytk/greenlet/You have new mail in /var/spool/mail/root

原因: pip 版本太低了,pip3 install --upgrade pip 更新到 pip-21.x 后重新安装就可以了。

[root@heheda xiaoqiang]# pip3 install --upgrade pip
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Cache entry deserialization failed, entry ignored
Collecting pip
  Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 34kB/s 
Installing collected packages: pip
Successfully installed pip-21.3.1
You are using pip version 21.3.1, however version 23.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@heheda xiaoqiang]# pip3 install sqlalchemy
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Collecting sqlalchemy
  Downloading SQLAlchemy-1.4.50-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 31 kB/s            
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from sqlalchemy) (4.8.3)
Collecting greenlet!=0.4.17
  Downloading greenlet-2.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (564 kB)
     |████████████████████████████████| 564 kB 23 kB/s            
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->sqlalchemy) (3.6.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->sqlalchemy) (4.1.1)
Installing collected packages: greenlet, sqlalchemy
Successfully installed greenlet-2.0.2 sqlalchemy-1.4.50
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual envi
ronment instead: https://pip.pypa.io/warnings/venvYou have new mail in /var/spool/mail/root

四、pip 常用命令

1. pip freeze

参考:pip 常用指令 pip freeze 命令用法介绍

  pip freeze 是一个用于生成 Python 项目所需依赖包列表的命令。它会列出所有已安装的包及其精确的版本号。这个命令非常有用,因为它可以帮助开发者确保他们的项目在不同的环境中都能正确运行。参数有:

  • -l,–local:只列出本地安装的包(即忽略用户站点目录中的包)。
  • -r,–requirement :使用给定的 requirements 文件作为基础,只列出在该文件中指定的包。
  • -f,–find-links :在生成的输出中,对于那些在给定 URL 中找到的包,添加对应的链接。
  • -a,–all:不仅列出顶级包,还列出所有子包。

  列出所有已安装的包及其版本号:pip freeze

  将所有已安装的包及其版本号输出到一个文件中:pip freeze > requirements.txt

  只列出本地安装的包:pip freeze --local

  使用给定的 requirements 文件作为基础,只列出在该文件中指定的包:pip freeze --requirement requirements.txt

  小结,pip freeze 是一个非常有用的命令,它可以帮助开发者生成他们的项目所需的依赖包列表。这样,当他们需要在另一个环境中运行他们的项目时,只需要使用 pip install -r requirements.txt 命令,就可以安装所有需要的包。

五、扩展:conda 命令

conda activate pyspark_env
conda install -c anaconda openpyxl
conda list | grep openpyxl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强签名设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值