conan-center-index
conan-center-index copied to clipboard
[question] Wrap conan imports with try/except
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?
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.
Let's check if we can do something else. I would try to avoid that at any cost.
Depends on https://github.com/conan-io/conan/issues/11239
I'd like to note the importance of this capability in environments like Yocto, where meta-conan lags behind the latest Conan releases significantly.
solved by https://github.com/conan-io/conan/pull/11908