簡體   English   中英

為什么我不能用 Pip 從 PyPI 安裝軟件包?

[英]Why can't I install packages from PyPI with Pip?

最近我一直無法使用 Pip 下載任何軟件包。 每次它以status 1出錯時,通常是在運行setup.py或構建輪子時。 以下是我得到的完整回溯的兩個示例:

pip install miniupnpc
Collecting miniupnpc
  Downloading miniupnpc-2.0.2.tar.gz (67 kB)
     |████████████████████████████████| 67 kB 1.7 MB/s
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for miniupnpc, since package 'wheel' is not installed.
Installing collected packages: miniupnpc
    Running setup.py install for miniupnpc ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\--------\AppData\Local\Programs\Python\Python310\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-kjrhsl01\\miniupnpc_31f9b0505dac4eff85fe9636cdb81aad\\setup.py'"'"'; __file__='"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-kjrhsl01\\miniupnpc_31f9b0505dac4eff85fe9636cdb81aad\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\--------\AppData\Local\Temp\pip-record-8d4qnr7v\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\--------\AppData\Local\Programs\Python\Python310\Include\miniupnpc'
         cwd: C:\Users\--------\AppData\Local\Temp\pip-install-kjrhsl01\miniupnpc_31f9b0505dac4eff85fe9636cdb81aad\
    Complete output (4 lines):
    running install
    running build
    running build_ext
    error: [WinError 2] The system cannot find the file specified
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\--------\AppData\Local\Programs\Python\Python310\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-kjrhsl01\\miniupnpc_31f9b0505dac4eff85fe9636cdb81aad\\setup.py'"'"'; __file__='"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-kjrhsl01\\miniupnpc_31f9b0505dac4eff85fe9636cdb81aad\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\--------\AppData\Local\Temp\pip-record-8d4qnr7v\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\--------\AppData\Local\Programs\Python\Python310\Include\miniupnpc' Check the logs for full command output.
Collecting conda
  Using cached conda-4.3.16.tar.gz (299 kB)
  Preparing metadata (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'C:\Users\--------\AppData\Local\Programs\Python\Python310\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-08mbbg7s\\conda_86cbd4f55db04b2aae91751f0497a4e2\\setup.py'"'"'; __file__='"'"'C:\\Users\\--------\\AppData\\Local\\Temp\\pip-install-08mbbg7s\\conda_86cbd4f55db04b2aae91751f0497a4e2\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\--------\AppData\Local\Temp\pip-pip-egg-info-6ybygnj4'
       cwd: C:\Users\--------\AppData\Local\Temp\pip-install-08mbbg7s\conda_86cbd4f55db04b2aae91751f0497a4e2\
  Complete output (17 lines):

  WARNING: Your current install method for conda only supports conda
  as a python library.  You are not installing a conda executable command
  or activate/deactivate commands.  If your intention is to install conda
  as a standalone application, currently supported install methods include
  the Anaconda installer and the miniconda installer.  If you'd still like
  for setup.py to create entry points for you, use `utils/setup-testing.py`.

  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Users\--------\AppData\Local\Temp\pip-install-08mbbg7s\conda_86cbd4f55db04b2aae91751f0497a4e2\setup.py", line 35, in <module>
      import conda._vendor.auxlib.packaging  # NOQA
    File "C:\Users\--------\AppData\Local\Temp\pip-install-08mbbg7s\conda_86cbd4f55db04b2aae91751f0497a4e2\conda\__init__.py", line 13, in <module>
      from .common.compat import iteritems, text_type
    File "C:\Users\--------\AppData\Local\Temp\pip-install-08mbbg7s\conda_86cbd4f55db04b2aae91751f0497a4e2\conda\common\compat.py", line 77, in <module>
      from collections import Iterable
  ImportError: cannot import name 'Iterable' from 'collections' (C:\Users\--------\AppData\Local\Programs\Python\Python310\lib\collections\__init__.py)
  ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/74/4e/c533c3136427be62c38cc0e038cabf167bb54489c2ced2f6df903c456861/conda-4.3.16.tar.gz#sha256=a91ef821343dea3ba9670f3d10b36c1ace4f4c36d70c175d8fc8886e94285953 (from https://pypi.org/simple/conda/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

我正在使用 Python 3.10.0 運行 Windows 10 64 位。 我有最新的pipwheel 我已經嘗試重新安裝 Python 和setuptools

每當您無法pip install包時,您的第一直覺應該是檢查其 PyPI 頁面。

我們以流行的 NumPy 包為例。 它的 PyPI 頁面是https://pypi.org/project/numpy 在其他信息中,它目前聲明:“要求:Python >=3.7,<3.11”。 這是撰寫本文時的最新版本1.21.4 以前的 NumPy 版本也將支持較舊的 Python 版本,例如1.18.0 ,它只需要“Python >=3.5”。

對於最新的 NumPy 版本,其開發者還指定 Python 版本需要低於 3.11。 作為預防措施。 在撰寫本文時,最新的 Python 版本是 Python 3.10。 換句話說,開發人員不保證這個特定的 NumPy 包將與 Python 的未來版本一起使用,但尚未發布。

NumPy 不是Python 包。 它包含用 C 或其他編程語言編寫的“擴展”,比 Python 本身“更接近金屬”。 這些擴展使 NumPy 變得更快。 但是它們必須被編譯為“本機代碼”,即特定於操作系統(Windows、Linux、macOS)以及平台架構(32 位、64 位、Apple M1、ARM 等)的代碼。

如果要從源代碼安裝 NumPy,則需要 C 編譯器。 在 Linux 上,已經安裝了該編譯器。 例如,在 Windows 上,您必須安裝 Visual Studio。 但可能還有其他“構建依賴項”,除了編譯器之外,您還需要安裝這些東西。 並且將取決於Python版本您正在構建/編譯軟件包 這是事情變得復雜的地方,最好避免這些並發症。

當 Pip 只在 PyPI 上找到一個“源代碼包”,並且該源代碼包包含本機代碼時,它會嘗試編譯它,即從源代碼構建它。 很多事情都可能在那里出錯。 出現問題時,Pip 通常會報告ERROR: Command errored out with exit status 1 ,如問題所示,盡管實際退出代碼可能不同。

總而言之,您通常希望在 PyPI 上找到預編譯的包。 特別是如果您使用的是 Windows。 在 Linux 和 macOS 上,事情往往會更順利地運行。 預編譯包也稱為二進制分發版,通常以“wheel”文件的形式.whl文件擴展名為.whl

要查看預編譯的輪子是否適用於您的 Python 版本和平台,請單擊 PyPI 頁面上的“下載文件”並檢查顯示的列表。 在問題中提到的 MiniUPnP 庫的情況下,目前在 2.2.3 版中可用,該列表位於https://pypi.org/project/miniupnpc/2.2.3/#files並包含這些.whl文件:

miniupnpc-2.2.3-cp27-cp27m-win32.whl
miniupnpc-2.2.3-cp35-cp35m-win32.whl
miniupnpc-2.2.3-cp36-cp36m-win32.whl
miniupnpc-2.2.3-cp36-cp36m-win_amd64.whl
miniupnpc-2.2.3-cp37-cp37m-win32.whl
miniupnpc-2.2.3-cp37-cp37m-win_amd64.whl
miniupnpc-2.2.3-cp39-cp39-win32.whl
miniupnpc-2.2.3-cp39-cp39-win_amd64.whl

值得注意的是,Python 3.10 沒有輪子,它的文件名中會有標簽cp310 如果有這樣的文件,Pip 基本上只會提取它(因為輪子只不過是美化的 zip 存檔)並將其內容復制到您的 Python 安裝中,無論它在您的機器上的任何位置。 但是這里安裝失敗,因為沒有輪子,Pip 甚至找不到要編譯的源代碼分發版,這將是一個.tar.gz文件。

對於conda包,它確實找到了源代碼發行conda-4.3.16.tar.gz ,但無法編譯它。 為什么它會失敗留給讀者作為練習。

更重要的是,wheels 作為一種打包格式,也用於純 Python 包。 然后不需要編譯。 比如PySerial庫PyPI頁面(目前)只列出了兩個安裝包:

pyserial-3.5-py2.py3-none-any.whl
pyserial-3.5.tar.gz

第一個是輪子,第二個是源分布。 在這種情況下,幾乎沒有區別。 作為純 Python 包,它不需要編譯。 這兩個文件基本上只包含 Python 源代碼,但除此之外沒有任何“復雜”。 這就是noneany標簽的含義。 即使庫尚未更新,在最新的 Python 版本或未來的 Python 版本上安裝它也不會出現問題。 (只要 Python 沒有在其自身的語法中引入任何向后不兼容的更改,這就是正確的。)

另一個要點是:如果您堅持使用最新的 Python 版本(無論哪個版本是最近發布的),那么請注意您要安裝的許多庫可能需要新版本。 也就是說,新的輪文件上傳到 PyPI。 也就是說,一個新版本。 開發人員通常要到幾個月后才會出現,即使是流行的庫也是如此。 所以可能會在以前的 Python 版本上多停留一會兒。 或者查找您知道需要的庫,並確保在升級之前已在 PyPI 上發布了新的輪子(如果需要)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM