conan-center-index icon indicating copy to clipboard operation
conan-center-index copied to clipboard

[question] Wrap conan imports with try/except

Open SpaceIm opened this issue 3 years ago • 3 comments

conan client is not able to check required_conan_version before python imports of a recipe (see https://github.com/conan-io/conan/issues/11239).

Due to migration of cci recipes to conan v2, many users may see cryptic import errors instead of a meaningful message suggesting the minimum conan version for this recipe.

So I suggest to use this kind of pattern:

try:
    from conan import ConanFile
    from conan.tools.cmake import CMakeToolchain, cmake_layout
    from conan.tools.files import copy, get, rmdir
except ImportError:
    try:
        from conan.errors import ConanException
    except ImportError:
        from conans.errors import ConanException
    raise ConanException("foo recipe requires conan 1.47.0 or higher.")
import os

required_conan_version = ">=1.47.0"

What are your thoughts?

SpaceIm avatar Aug 09 '22 20:08 SpaceIm

I could say users should be running the latest version, as expected on CCI, but the truth is that not all people are able to update their conan version. Also, Conan client could parse it first and output a message in case the version does not fit, but it would require a new release, and that idea was denied in the past by Conan core devs.

Besides our efforts, we still see people reporting "bug" because their Conan client version is outdated, so, yes: The try..except could our best choice, as we don't have a 2to3 or future equivalent to solve conan imports.

uilianries avatar Aug 10 '22 07:08 uilianries

Let's check if we can do something else. I would try to avoid that at any cost.

lasote avatar Aug 10 '22 08:08 lasote

Depends on https://github.com/conan-io/conan/issues/11239

jgsogo avatar Aug 11 '22 09:08 jgsogo

I'd like to note the importance of this capability in environments like Yocto, where meta-conan lags behind the latest Conan releases significantly.

jwillikers avatar Oct 27 '22 13:10 jwillikers

solved by https://github.com/conan-io/conan/pull/11908

SpaceIm avatar Oct 29 '22 20:10 SpaceIm