python 安装 fitz pip安装模块时提示: No module named pip

fitz 是 PyMuPDF 库中的一个子库,用于处理 PDF 文档。用 pip 安装 PyMuPDF后, 就能使用 import 导入 fitz 了。

只需要一句命令:

pip install PyMuPDF -i https://pypi.tuna.tsinghua.edu.cn/simple

安装已完成。

下文关于可能出现的问题和疑惑解析 ,可以不看了

问题:使用 pip 安装 PyMuPD 模块时提示: No module named pip

修复 pip 安装

存在 Python2、 Python3 多版本的系统中指定版本环境, 下文以系统中只有 Python3 为例

  • 1、在 Python 3.4 + 中
Microsoft Windows [版本 10.0.19045.2728]
(c) Microsoft Corporation。保留所有权利。
 
C:\WINDOWS\system32>python -m ensurepip
Installing collected packages: pip
Successfully installed pip-22.3

ensurepip 包支持将 pip 安装程序引导到现有的 Python 安装或虚拟环境中。

  • 2、在 Python 3 ≤ 3.3 中

https://bootstrap.pypa.io/get-pip.py 下载脚本。

打开终端/命令提示符,到包含该文件的文件夹并运行:

 py get-pip.py

查看 pip 版本 pip -V

Microsoft Windows [版本 10.0.19045.2728]
(c) Microsoft Corporation。保留所有权利。

C:\WINDOWS\system32>pip -V
pip 23.0.1 from C:\Python312\Lib\site-packages\pip (python 3.12)

有博文中提到也可以使用 easy_install 来安装 pip

easy_install pip

easy_install 和 pip 在 linux 和 Windows 中的区别

easy_install 和 pip 是 Python 中常用的包管理工具,虽然二者的功能类似,但在 Linux 和 Windows 中还是有一些区别的。

在 Linux 中:

使用方式:easy_install 和 pip 在命令行中使用方式相似,但 pip 的命令更简洁,易于使用。

  • 安装方式不同

easy_install 是 Python 默认安装的包管理工具,可以通过以下命令进行安装:

$ sudo apt-get install python-setuptools

pip 可以通过以下命令进行安装:

$ sudo apt-get install python-pip

  • 安装包的方式不同

easy_install 安装包的方式如下:

$ sudo easy_install package_name

pip 安装包的方式如下:

$ sudo pip install package_name

  • 配置文件不同

easy_install 的配置文件位于 ~/.pydistutils.cfg,而 pip 的配置文件位于 ~/.pip/pip.conf。

在 Windows 中:

安装方式:easy_install 已经内置在 Python 中,不需要额外安装,而 pip 需要单独安装。

参考上文中的 修复 pip 安装

pip 安装包的方式如下:

在命令行运行

pip install package_name

使用方式:pip 的命令行使用方式更加简单明了,可以快速完成包的安装、升级、卸载等操作;而 easy_install 的使用方式相对来说更加繁琐,需要手动指定参数。

easy_install 和 pip 在 linux 和 Windows 中的相同点

包管理:pip 能够自动解决依赖关系,支持从 PyPI 中安装和卸载包,支持安装本地和远程包等;而 easy_install 的依赖解决方案相对简单,也支持安装本地和远程包,在解决依赖关系时,无法处理一些复杂的依赖关系,easy_install 只考虑直接依赖关系,而不考虑间接依赖关系, 如果存在多个版本的同一个依赖项,easy_install 也无法处理。

版本兼容性:pip 只支持 Python 2.7 和 Python 3.4 及以上版本,而 easy_install 支持 Python 2.x 和 Python 3.x 版本。

综上所述,pip 在 Linux 和 Windows 中都比 easy_install 更加强大和方便,建议使用 pip 来管理 Python 包,它的依赖解决方案更加完善,可以自动解决复杂的依赖关系。

更新 pip

python环境在安装源文件时使用默认安装很慢,使用国内清华源速度比较快

C:\WINDOWS\system32>python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pip in c:\python311\lib\site-packages (22.3)
Collecting pip
  Using cached pip-23.0.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.3
    Uninstalling pip-22.3:
      Successfully uninstalled pip-22.3
Successfully installed pip-23.0.1

安装 PyMuPDF

C:\WINDOWS\system32>pip install PyMuPDF -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting PyMuPDF
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/aa/c0/2a7bfe118a5c965aa358da95fe77872d8d4fabadd14ed5f46a55b6ce7cc6/PyMuPDF-1.21.1-cp311-cp311-win_amd64.whl (11.7 MB)
     ---------------------------------------- 11.7/11.7 MB 203.3 kB/s eta 0:00:00
Installing collected packages: PyMuPDF
Successfully installed PyMuPDF-1.21.1
 

查看 PyMuPDF 包状态,正常

C:\WINDOWS\system32>pip list
Package            Version
------------------ ----------
PyMuPDF            1.21.1

  • 安装最新版本
    pip install PyMuPDF

  • 安装指定版本
    pip install PyMuPDF==

查看 PyMuPDF 包组件 fitz 状态,正常

C:\WINDOWS\system32>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

_testcapi           fitz                queue               zipimport

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".

>>>

测试导入,正常

C:\WINDOWS\system32>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fitz
>>>

PyMuPDF 使用文档 PyMuPDF Documentation
https://buildmedia.readthedocs.org/media/pdf/pymupdf/latest/pymupdf.pdf

使用 pip 安装 fitz

C:\WINDOWS\system32>pip install fitz -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting fitz
  Downloading 
     --------------------------------------
Installing collected packages: fitz

Successfully installed fitz-0.0.1.dev2

查看 pip 安装的 fitz 库

C:\WINDOWS\system32>pip list
Package            Version
------------------ ----------
fitz               0.0.1.dev2

用 PyMuPDF 安装 fitz 与 用 pip 安装 fitz 的区别

  • 无论是用 pip 安装 fitz 还是用 PyMuPDF 安装 fitz,都可以使用 fitz 库的功能。

  • 使用 pip 安装 fitz 只能安装 fitz 库本身,而不包括 PyMuPDF 库的其他功能。这意味着,如果需要使用 PyMuPDF 库的其他功能,需要再安装 PyMuPDF 库。而安装 PyMuPDF 库会自动安装 fitz 子库,可以使用 PyMuPDF 库包括 fitz 子库的所有功能。

  • 如果用 pip uninstall 卸载 pip 安装的 fitz,会把 PyMuPDF 包组件中 fitz 一并删除,实际上两种安装方式都在同一位置。

fitz 库更新,可以通过重装 PyMuPDF 来完成


C:\WINDOWS\system32>pip install --force-reinstall PyMuPDF -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting PyMuPDF
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/aa/c0/2a7bfe118a5c965aa358da95fe77872d8d4fabadd14ed5f46a55b6ce7cc6/PyMuPDF-1.21.1-cp311-cp311-win_amd64.whl (11.7 MB)
Installing collected packages: PyMuPDF
  Attempting uninstall: PyMuPDF
    Found existing installation: PyMuPDF 1.21.1
    Uninstalling PyMuPDF-1.21.1:
      Successfully uninstalled PyMuPDF-1.21.1
Successfully installed PyMuPDF-1.21.1

再次查看 PyMuPDF 包组件 fitz 状态,正常

C:\WINDOWS\system32>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

_symtable           fitz                pty                 winreg

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".

>>>

本文属于作者原创,转载请注明出处。

猜你喜欢

转载自blog.csdn.net/u013669912/article/details/129622828