使用 pip 在开发模式下安装本地包时找不到 setuptools

setuptools not found when installing local package in development mode with pip

我正在开发 Python 包,并希望使用 pip 在开发模式下安装它。当我 运行 pip install 时,我得到一个 ModuleNotFoundError 告诉我缺少 setuptools

$ pip install -e my-package/
...
  Running setup.py develop for my-package
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/user/my-package/setup.py'"'"'; __file__='"'"'/home/user/my-package/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'"'"'))' develop --no-deps --user --prefix=
         cwd: /home/user/my-package/
    Complete output (3 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ModuleNotFoundError: No module named 'setuptools'

这让我很困惑,因为我确实有安装工具:

$ sudo apt-get install python3-setuptools
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-setuptools is already the newest version (45.2.0-1).
The following packages were automatically installed and are no longer required:
  espeak-ng-data gir1.2-gstreamer-1.0 libao-common libao4 libaudio2 libdotconf0 libespeak-ng1
  libfprint-2-tod1 libllvm9 libllvm9:i386 libpcaudio0 libsonic0 libspeechd2 python3-brlapi python3-click
  python3-colorama python3-louis python3-pyatspi python3-speechd sound-icons
  speech-dispatcher-audio-plugins xbrlapi
Use 'sudo apt autoremove' to remove them.
0 to upgrade, 0 to newly install, 0 to remove and 231 not to upgrade.

我可以正常导入它而不会出错 python:

$ python
Python 3.9.5 (default, Nov 23 2021, 15:27:38) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import setuptools
>>> 

我什至可以调用命令中出错的确切部分,而我自己不会出错!

$ /usr/bin/python -c 'import io, os, sys, setuptools, tokenize'
$

为什么 pip 找不到设置工具?还值得注意的是 pip install my-package 在没有 -e 开发模式标志的情况下工作。

我的 python/pip 个可执行文件:

$ which python
/usr/bin/python            [a symlink to /usr/bin/python3.9]
$ which python3
/usr/bin/python3           [a symlink to /usr/bin/python3.8]
$ which pip
/home/user/.local/bin/pip
$ which pip3
/home/user/.local/bin/pip3

我以前遇到过这个问题,以下步骤对我有用。

  1. 使用 pip 安装 setuptools
    Pypi 页面:https://pypi.org/project/setuptools/.

    python -m pip install setuptools
    
  2. 确认 setuptools 已安装。

    python -m pip list
    

  3. 再次尝试重新安装您的软件包。

    python -m pip install -e my-package/
    

正在执行:

sudo python setup.py develop

包目录终于成功了。我无法解释为什么其他方法不起作用,但这是解决我的问题的方法。