python - nrfutil - Nixos 上的 "ImportError: No module named main"

标签 python pip nixos

我正在使用用 Python 实现的工具 nrfutil。为了能够在 NixOS 下使用它,我使用了一个 default.nix 文件,该文件将 nrfutil 安装到 venv 中。这在一段时间以来效果非常好。 (在 alpine 容器中使用 Nix 在构建服务器上进行的最后一次构建可以成功构建我 11 天前正在开发的软件。)当我执行完全相同的操作(即重新启动 CI 服务器构建而不进行更改)时,构建失败现在提示 pip 不正确:

$ nix-shell 
New python executable in /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7
Not overwriting existing python script /home/matthias/source/tbconnect/bootloader/.venv/bin/python (you must use /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7)
Installing pip, wheel...
done.
Traceback (most recent call last):
  File "/home/matthias/source/tbconnect/bootloader/.venv/bin/pip", line 6, in <module>
    from pip._internal.main import main
ImportError: No module named main

对我来说,模块 main 应该存在:

$ ls -l .venv/lib/python2.7/site-packages/pip/_internal/main.py
-rw-r--r-- 1 matthias matthias 1359 10月 15 12:27 .venv/lib/python2.7/site-packages/pip/_internal/main.py

我对Python环境不太了解,所以我不知道更多。有人指 pip 我在哪里继续调试吗? Python 如何解析模块?为什么它找不到我看来存在的模块?

这是我用来安装 pipdefault.nix:

with import <nixpkgs> {};
with pkgs.python27Packages;

stdenv.mkDerivation {
  name = "impurePythonEnv";
  buildInputs = [
    automake
    autoconf
    gcc-arm-embedded-7
    # these packages are required for virtualenv and pip to work:
    #
    python27Full
    python27Packages.virtualenv
    python27Packages.pip
    # the following packages are related to the dependencies of your python
    # project.
    # In this particular example the python modules listed in the
    # requirements.txt require the following packages to be installed locally
    # in order to compile any binary extensions they may require.
    #
    taglib
    openssl
    git
    stdenv
    zlib ];
  src = null;
  shellHook = ''
    # set SOURCE_DATE_EPOCH so that we can use python wheels
    SOURCE_DATE_EPOCH=$(date +%s)
    virtualenv --no-setuptools .venv
    export PATH=$PWD/.venv/bin:$PATH
    #pip install nrfutil
    pip help

    # the following is required to build micro_ecc_lib_nrf52.a in the SDK
    export GNU_INSTALL_ROOT="${gcc-arm-embedded-7}/bin/"

    unset CC
  '';
}
  • 我将 pip install nrfutil 替换为 pip help,以确保问题不是我尝试安装的软件包本身造成的。
  • 我仍在使用 python 2.7,因为 nrfutil 仍然不适合 Python 3。
  • 无论如何,用 python37 替换 python27 并没有改变我在尝试启动 pip 时遇到的错误。)
  • 本地使用的 NixOS 版本是 19.09。 CI docker 容器中的 Nix 是 nixos/nix:latest,它是 Alpine Linux 上的 nix 包管理器。

更新:

实际上,当我用 python2.7 -m pip install nrfutil 替换对 pip install nrfutil 的调用时,它就可以工作了。这实际上让我更加困惑。 python2.7 正是 pip shebang 中的二进制文件:

[nix-shell:~/source/tbconnect/bootloader]$ type python2.7
python2.7 is /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7

[nix-shell:~/source/tbconnect/bootloader]$ type pip
pip is /home/matthias/source/tbconnect/bootloader/.venv/bin/pip

[nix-shell:~/source/tbconnect/bootloader]$ head --lines 2 .venv/bin/pip
#!/home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7
# -*- coding: utf-8 -*-

更新 2: 我发现解决问题的另一种方法是编辑 .venv/bin/pip。该脚本尝试了以下导入:

from pip._internal.main import main

我认为这是从 pip 19.3 开始的新模块路径。但我还有 pip 19.2。当我将此行更改为:

from pip._internal import main

通过输入 pip 运行 pip 可以正常工作。

问题是我不知道为什么pip脚本试图加载新的模块路径,而NixOS仍然有旧版本的pip

我还在 GitHub 上提出了 NixOS 的问题:https://github.com/NixOS/nixpkgs/issues/71178

最佳答案

我通过删除 Python27Packages.pip 让你的 shell 派生工作,

(nix-shell) 2d [azul:/tmp/lixo12333] $ 
 >>> pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Package          Version
---------------- -------
behave           1.2.6  
Click            7.0    
crcmod           1.7    
ecdsa            0.13.3 
enum34           1.1.6  
future           0.18.2 
intelhex         2.2.1  
ipaddress        1.0.23 
libusb1          1.7.1  
linecache2       1.0.0  
nrfutil          5.2.0  
parse            1.12.1 
parse-type       0.5.2  
pc-ble-driver-py 0.11.4 
piccata          1.0.1  
pip              19.3.1 
protobuf         3.10.0 
pyserial         3.4    
pyspinel         1.0.0a3
PyYAML           4.2b4  
setuptools       41.6.0 
six              1.12.0 
tqdm             4.37.0 
traceback2       1.4.0  
virtualenv       16.4.3 
wheel            0.33.6 
wrapt            1.11.2 
(nix-shell) 2d [azul:/tmp/lixo12333] $ 

和我的default.nix

with import <nixpkgs> {};
with pkgs.python27Packages;

stdenv.mkDerivation {
  name = "impurePythonEnv";
  buildInputs = [
    automake
    autoconf
    gcc-arm-embedded-7
    # these packages are required for virtualenv and pip to work:
    #
    python27Full
    python27Packages.virtualenv
    # the following packages are related to the dependencies of your python
    # project.
    # In this particular example the python modules listed in the
    # requirements.txt require the following packages to be installed locally
    # in order to compile any binary extensions they may require.
    #
    taglib
    openssl
    git
    stdenv
    zlib ];
  src = null;
  shellHook = ''
    # set SOURCE_DATE_EPOCH so that we can use python wheels
    SOURCE_DATE_EPOCH=$(date +%s)
    virtualenv .venv
    export PATH=$PWD/.venv/bin:$PATH
    pip install nrfutil
    #pip help

    # the following is required to build micro_ecc_lib_nrf52.a in the SDK
    export GNU_INSTALL_ROOT="${gcc-arm-embedded-7}/bin/"

    unset CC
  '';
}

关于python - nrfutil - Nixos 上的 "ImportError: No module named main",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392710/

相关文章:

python - Visual Studio Code - Python 中的输入函数

python - 检查是否安装了pip?

深度学习环境下Python包安装错误

python - 强制所有列都是 pandas 中的字符串

python - 无法在 Windows 8 或 10 的特定窗口上使用 pywinauto 单击按钮

python - 运行 import tensorflow 后的非法指令(核心转储)

nixos - 如何将系统升级到nixos-unstable?

nixos - 如何在不抛出 `cannot coerce a set to a string` 的情况下覆盖 Nix 导数?

gcc - 如何在 NixOS 上生成静态可执行文件?

python - 直接从 numpy 进行 h.264 编码