简体   繁体   中英

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

Recently I haven't been able to download any packages with Pip. Every time it errors out with status 1 , usually while running setup.py or building wheel. Here are two examples of the full traceback that I get:

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.

I'm running Windows 10 64-bit with Python 3.10.0. I have up to date pip and wheel . I've already tried reinstalling Python and setuptools .

Whenever you cannot pip install a package, your first instinct should be to check its PyPI page.

Let's take the popular NumPy package as an example. Its PyPI page is https://pypi.org/project/numpy . Among other information, it currently states: "Requires: Python >=3.7, <3.11". That's for the latest version, 1.21.4 as of this writing. Previous NumPy versions would also support older Python versions, such as 1.18.0 which requires only "Python >=3.5".

For the latest NumPy version, its developers have also specified that the Python version needs to be less than 3.11. As a precaution. The latest Python version, as of this writing, is Python 3.10. In other words, the developers make no guarantees that this particular NumPy package will work with future versions of Python, yet to be released.

NumPy is not a pure Python package. It contains "extensions" written in C or other programming languages that are "closer to the metal" than Python itself. These extensions are what makes NumPy fast. But they have to be compiled to "native code", ie code specific to an operating system (Windows, Linux, macOS) as well as the platform architecture (32-bit, 64-bit, Apple M1, ARM, etc.).

If you were to install NumPy from source, you'd need a C compiler. On Linux, that compiler is already installed. On Windows, you'd have to install Visual Studio, for example. But there may also be other "build dependencies", things you need to have installed in addition to the compiler. And which will depend on the Python version you're building/compiling the package for . This is where things get complicated, and it's best to avoid these complications.

When Pip only finds a "source package" on PyPI, and that source package contains native code, it tries to compile it, ie build it from source. Lots of things can go wrong there. When things go wrong, Pip will usually report ERROR: Command errored out with exit status 1 , as in the question, though the actual exit code could be a different one.

This is all a long way of saying that you usually want to find a pre-compiled package on PyPI. Especially if you're on Windows. Things tend to work more smoothly on Linux, and possibly macOS. Pre-compiled packages are also known as binary distributions and are typically distributed as "wheel" files, with a .whl file extension.

To see if a pre-compiled wheel is available for your Python version and platform, click on "Download files" on the PyPI page and inspect the list that is displayed. In the case of the MiniUPnP library mentioned in the question, currently available in version 2.2.3, the list is found at https://pypi.org/project/miniupnpc/2.2.3/#files and includes these .whl files:

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

Notably, there is no wheel for Python 3.10, which would have the tag cp310 in the file name. If there was such a file, Pip would essentially just extract it (as wheels are nothing but glorified zip archives) and copy its content over to your Python installation, wherever it may be on your machine. But here the installation fails because there is not wheel and Pip cannot even find a source distribution to compile from, which would be a .tar.gz file.

For the conda package, it does find the source distribution, conda-4.3.16.tar.gz , but fails to compile it. Why it fails is left as an exercise to the reader.

More importantly, wheels, as a packaging format, are also used for pure-Python packages. And then don't require compilation. For example, the PyPI page for the PySerial library (currently) only lists two installation packages:

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

The first is a wheel, the second the source distribution. In this case, there is little difference. As a pure-Python package, it doesn't need to be compiled. Both files essentially just include the Python source code, but nothing "complicated" beyond that. Which is what the none and any tags imply. You should expect no problems installing this on the latest Python version, or a future Python version, even if the library hasn't been updated. (This is true for as long as Python does not introduce any backwards-incompatible changes in its own syntax.)

The other take-away from this is: If you insist on working with the latest Python version, whichever one just recently came out, then be aware that lots of libraries you want to install may require new builds. That is, new wheel files uploaded to PyPI. That is, a new release. Which developers often don't come around to until a couple of months in, even for popular libraries. So maybe stay on the previous Python version just a little while longer. Or look up the libraries you know you'll need and make sure new wheels (if needed) have been published on PyPI before you upgrade.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM