【问题标题】:How to resolve Python package dependencies with pipenv?如何使用 pipenv 解决 Python 包依赖关系?
【发布时间】:2019-01-03 13:01:08
【问题描述】:

我正在使用pipenv 来处理 Python 包依赖项。

Python 包正在使用两个包(名为 pckg1pckg2),它们依赖于名为 pckg3 的同一包,但来自两个不同的版本。显示依赖树:

$ pipenv graph
  pckg1==3.0.0
    - pckg3 [required: >=4.1.0]
  pckg2==1.0.2
    - pckg3 [required: ==4.0.11]

尝试安装依赖项:

$ pipenv install

Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
You can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
Could not find a version that matches pckg3==4.0.11,==4.1.0,>=4.1.0 (from -r C:\Users\user\AppData\Local\Temp\pipenv-o7uxm080-requirements\pipenv-hwekv7dc-constraints.txt (line 2))
Tried: 3.3.1, 3.3.2, 3.3.3, 3.4.0, 3.4.2, 4.0.0, 4.0.0, 4.0.1, 4.0.1, 4.0.2, 4.0.2, 4.0.3, 4.0.3, 4.0.4, 4.0.4, 4.0.6, 4.0.6, 4.0.8, 4.0.8, 4.0.9, 4.0.9, 4.0.10, 4.0.10, 4.0.11, 4.0.11, 4.1.0, 4.1.0, 4.1.1, 4.1.1, 4.1.2, 4.1.2, 4.2.1, 4.2.1, 4.3.0, 4.3.0
There are incompatible versions in the resolved dependencies.

按照建议,pip install --skip-lock 可以解决问题,但依赖关系树仍未解决。

我很想告诉Pipenv 覆盖pckg2 的要求,并指定pckg3>=4.1.0

如何解决?

【问题讨论】:

    标签: python python-3.x pip dependencies pipenv


    【解决方案1】:

    我经常收到这个错误。每次清除锁定文件中的缓存都很有效。

    $ pipenv lock --pre --clear

    【讨论】:

    • 这很有帮助,但我必须在没有 --pre 的情况下这样做,否则我会遇到其他不匹配错误。
    • 将@northtree 与 Harsha Goli 结合起来就成功了……但仍然没有真正了解根本问题是什么:(
    • 这通常意味着与 pipfile.lock 存在无效的不一致。有时,如果存在依赖冲突,这可能会真正发生 - 但这个答案是针对 pipenv 只需要转储它的缓存并重新创建锁定文件时量身定制的
    • 在我的 Mac 上我不能使用 pipenv lock --pre --clear 但它只有在我说 python3.9 -m pipenv lock --pre --clear 有帮助时才有效?
    • 在我自己的情况下,当我运行 pipenv lock --pre --clear 时出现依赖错误。
    【解决方案2】:

    你不能。目前,pipenv 不提供任何内容来显式覆盖需求约束。

    作为一种解决方法,您可以将要覆盖的依赖项放在 dev-packages 中,因为这些依赖项将被 packages 覆盖,所以这个 Pipfile 应该安装 pckg3>=4.1.0

    # Pipfile
    ...
    [packages]
    pckg1 = "==3.0.0"
    
    [dev-packages]
    pckg2 = "==1.0.2"
    

    如果您现在锁定并安装:

    $ pipenv lock --dev
    $ pipenv install --dev
    

    ==4.0.11 的要求将被 >=4.1.0 覆盖。如果你问我这很难看,因为这不是开发包的用途,而且你正在改变项目中 pckg2 依赖项的角色,但我在这里没有看到更好的方法。

    【讨论】:

    • 覆盖dev-package 中的依赖项有效,但我真的不喜欢将非开发包放在开发包列表中......
    • 我同意这很臭(在答案中提到了这一点),但恐怕pipenv 目前的发展状态没有其他选择......
    • @hoefling 我不明白你的解释中 3.0.01.0.2 是 yaml 与 ==4.0.11 >=4.1.0 的关系
    • @abhiieor 查看问题中pipenv graph 的输出 - 前两个是直接依赖的版本,后两个是相同瞬时依赖的冲突版本。总的来说,这个答案可能已经过时了,我不再使用pipenv了。
    【解决方案3】:

    当 pipfile 上有未完成的例程时,此方法有效。

    一旦我犯了错误并运行

    pipenv install codecove # With an 'e' at the end
    

    并且 pipenv 一直试图完成安装但没有成功,因为 lib 不存在。我解决了:

    pipenv uninstall codecove
    

    之后安装了codecov。

    我试着跑了

    pipenv lock --clear
    pipenv lock --pre --clear
    

    但只有在卸载了错误名称的库后我才成功。

    【讨论】:

    • 是的,这很愚蠢!即使我尝试安装pipenv install WrongName:当然,它会安装失败,但另外它会使pipenv 之后无法安装所有东西!所以我需要在那之后做:pipenv uninstall WrongName...
    【解决方案4】:

    google-cloud-core 也有类似的问题。

    $ pipenv lock
    Locking [dev-packages] dependencies…
    Locking [packages] dependencies…
    
    Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
      First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
     Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
      Hint: try $ pipenv lock --pre if it is a pre-release dependency.
    Could not find a version that matches google-cloud-core<0.29dev,<0.30dev,>=0.28.0,>=0.29.0
    Tried: 0.20.0, 0.20.0, 0.21.0, 0.21.0, 0.22.0, 0.22.0, 0.22.1, 0.22.1, 0.23.0, 0.23.0, 0.23.1, 0.23.1, 0.24.0, 0.24.0, 0.24.1, 0.24.1, 0.25.0, 0.25.0, 0.26.0, 0.26.0, 0.27.0, 0.27.0, 0.27.1, 0.27.1, 0.28.0, 0.28.0, 0.28.1, 0.28.1, 0.29.0, 0.29.0
    There are incompatible versions in the resolved dependencies.
    

    解决了

    1. rm -rf Pipfile.lock
    2. pipenv 更新

    【讨论】:

      【解决方案5】:

      如果您收到如下错误:

      Locking [dev-packages] dependencies…
      Locking [packages] dependencies…
      ✘ Locking Failed! 
      

      对我来说,这是因为底层虚拟环境没有引用我当前的目录。

      我通过将内容移动到新目录并删除旧目录来解决了这个问题。

      我还必须删除PipfilePipfile.lock,但我不确定这是否有必要。

      【讨论】:

      • 这对我有用……但我感觉不太好。哈。
      • @HashRocketSyntax 是的,我同意。希望我对潜在问题有透明度
      【解决方案6】:

      这也发生在我身上,我正在使用 Visual Studio Code 并且我能够摆脱它:

      • 从您的 python 文件夹中删除除 *.py 之外的所有文件(.vscode、pycache、* 该文件夹中的虚拟环境)
      • 创建一个新的 virtualenv env(name)
      • new\scripts\activate.bat

      然后尝试为虚拟文件夹安装缺少的依赖项,一切顺利,它对我有用。

      【讨论】:

        【解决方案7】:

        如果遇到Locking Failed! 错误:

        Locking [dev-packages] dependencies…
        Locking [packages] dependencies…
        ✘ Locking Failed!
        

        清除 pipfile.lock 上的缓存卸载您的软件包并重新启动 VM 对我有用。

        试试:

        pipenv uninstall *YourPackage*
        pipenv uninstall *YourPackage* --dev   ##(if relevant to your package)
        pipenv lock --clear
        

        停止并销毁虚拟机

        重启虚拟机

        希望这会有所帮助!

        【讨论】:

          【解决方案8】:
          pip install -U pipenv     
          

          为我解决了在 Ubuntu 21.04 - Hirsute Hippo - python 版本 3.9.5

          这个命令安装了那些包:

          • appdirs-1.4.4
          • certifi-2021.5.30
          • distlib-0.3.2
          • filelock-3.0.12
          • pipenv-2021.5.29
          • 六-1.16.0
          • virtualenv-20.4.7
          • virtualenv-clone-0.5.4

          【讨论】:

            【解决方案9】:

            glob 模块也有类似的问题。我从 Pipfile 中删除了glob = "*",之后它运行良好。

            错误:

            bash-4.2# pipenv lock  --clear
            Locking [dev-packages] dependencies…
            ✔ Success! 
            Locking [packages] dependencies…
            ✘ Locking Failed! 
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/resolver.py", line 69, in resolve
            [pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 726, in resolve_deps
            [pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
            [pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 395, in resolve
            [pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
            [pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches glob
            [pipenv.exceptions.ResolutionFailure]:       No versions found
            [pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
              First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
             Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
              Hint: try $ pipenv lock --pre if it is a pre-release dependency.
            ERROR: ERROR: Could not find a version that matches glob
            No versions found
            Was https://pypi.org/simple reachable?
            [pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 726, in resolve_deps
            [pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
            [pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
            [pipenv.exceptions.ResolutionFailure]:   File "/root/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pipenv/utils.py", line 395, in resolve
            [pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
            [pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches glob
            [pipenv.exceptions.ResolutionFailure]:       No versions found
            [pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
              First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
             Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
              Hint: try $ pipenv lock --pre if it is a pre-release dependency.
            ERROR: ERROR: Could not find a version that matches glob
            No versions found
            Was https://pypi.org/simple reachable?
            

            【讨论】:

              【解决方案10】:

              在使用 VS Code 的 Windows 10 上,我通过在 Powershell 中运行 pipenv 进行了很多混乱后得到了全新安装。我还删除了之前尝试的所有痕迹(新目录删除了之前的 venvs)。

              【讨论】:

                【解决方案11】:

                这里没有什么对我有用。最后解决了这个问题:

                pip uninstall pipenv
                pip install pipenv
                

                【讨论】:

                  【解决方案12】:
                  pipenv lock
                  Locking [dev-packages] dependencies…
                  Locking [packages] dependencies…
                  
                  Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
                    First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
                   Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
                    Hint: try $ pipenv lock --pre if it is a pre-release dependency.
                  

                  pipenv 安装 --clean

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2019-01-01
                    • 1970-01-01
                    • 2022-11-09
                    • 2012-05-20
                    • 2012-01-31
                    • 2023-03-11
                    • 1970-01-01
                    相关资源
                    最近更新 更多