简体   繁体   English

nrfutil - Nixos 上的“ImportError: No module named main”

[英]nrfutil - “ImportError: No module named main” on Nixos

I'm using the tool nrfutil which is implemented in Python.我正在使用在nrfutil中实现的工具 nrfutil。 To be able to use it under NixOS I was using a default.nix file, that installed nrfutil into a venv.为了能够在 NixOS 下使用它,我使用了一个default.nix文件,它将 nrfutil 安装到一个 venv 中。 This worked for some time very well.这工作了一段时间。 (The last build on the build server using Nix within an alpine container could build the software I'm working on 11 days ago successfully.) When I do exactly the same things (ie restarting the CI server build without changes), the build fails now complaining about pip being incorrect: (在 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

To me it seems that the module main should exist:在我看来,模块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

I'm not very much into the Python environment, so I don't know any further.我不太了解 Python 环境,所以我不知道更多。 Has somebody any pointer for me where to continue debugging?有人告诉我在哪里继续调试吗? How is Python resolving modules? Python 是如何解析模块的? Why doesn't it find the module, that seems to be present to me?为什么它没有找到似乎存在于我面前的模块?

This is my default.nix that I use to install pip :这是我用来安装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
  '';
}
  • I replaced pip install nrfutil with pip help to make sure the problem is not the package I try to install itself.我将pip install nrfutil替换为pip help确保问题不是 package 我尝试自行安装。
  • I'm still using python 2.7 as the nrfutil still is not fit for Python 3.我仍在使用 python 2.7,因为nrfutil仍然不适合 Python 3。
  • Anyway replacing python27 with python37 did not change the error I get when trying to start pip .)无论如何用python27替换python37并没有改变我在尝试启动pip时遇到的错误。)
  • NixOS version used locally is 19.09.本地使用的 NixOS 版本是 19.09。 Nix in the CI docker container is nixos/nix:latest which is the nix package manager on Alpine Linux. CI docker 容器中的 Nix 是nixos/nix:latest ,它是 Alpine Linux 上的 nix package 管理器。

Update:更新:

Actually it works when I replace the call to pip install nrfutil with python2.7 -m pip install nrfutil .实际上,当我用python2.7 -m pip install nrfutil替换对pip install nrfutil的调用时,它会起作用。 This actually confuses me even more.这实际上让我更加困惑。 python2.7 is exactly the binary that is in the shebang of pip: 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 -*-

Update 2: I found out that another way to fix the problem is to edit .venv/bin/pip .更新 2:我发现解决问题的另一种方法是编辑.venv/bin/pip This script tried the following import:该脚本尝试了以下导入:

from pip._internal.main import main

Which I think is the new module path starting with pip 19.3.我认为这是从 pip 19.3 开始的新模块路径。 But I still have pip 19.2.但我还有 pip 19.2。 When I change this line to:当我将此行更改为:

from pip._internal import main

Running pip by typing pip is working.通过键入pip运行pip正在运行。

The thing is I have no idea why the pip script is trying to load the new module path while NixOS still has the old version of pip .问题是我不知道为什么pip脚本试图加载新的模块路径,而 NixOS 仍然有旧版本的pip

I also opened an issue for NixOS on GitHub: https://github.com/NixOS/nixpkgs/issues/71178我还在 GitHub 上为 NixOS 打开了一个问题: https://github.com/NixOS/nixpkgs/issues/71178

I got your shell derivation to work by dropping the Python27Packages.pip ,我通过删除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] $ 

and my default.nix和我的 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
  '';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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