I am trying to introduce conan into an existing project. It will be a consumer from conan's perspective. The project currently has a "configure" script that sets the environment up to build, and one of the steps it performs is functionally similar to what conan install
would perform.
This script is written in python. It seems silly for me run conan install
shell commands from within a python script, especially since I also plan on using the python variant of conanfile.py
in order to facilitate better integration with a bespoke in-house build system (we are trying to phase it out otherwise I might consider writing a custom generator).
What are the recommended practices for accessing conan functionality directly from a python script? Is there a python API that's pseudo-stable (as stable as it can be in an 0.24.0 release level)?
Yes, it started two releases ago, you can check the current python API: https://github.com/conan-io/conan/blob/develop/conans/client/conan_api.py
This is very ongoing work, some methods might still print text to output instead of a formal return value, and it will be subject of changes, we will be adding the "create" behavior, etc. But indeed it is intended so that other python apps can use it instead of calling the shell commands, and the interface should be usable just by looking to the source code. We started to version it, but that won't be effectively used until we get out of beta.
Whats your opinion about an approach like this example?
from conans.client.command import main
import sys
args = ["test_package",
"-s", "build_type=Release"
# etc.
]
main(args)
@marausch
It might work for simple cases, but will fail for example when some return is expected. Lets say, conan search
will print to out the result. If using the new conan_api
it will return a list with the matching package references to the search.
The conan_api has other advantages, like exploded method signatures, so easier to use than bare command line.
So yes, your approach might work and be ok for many cases, but conan_api
would be more convenient for others.
I would prefer to have a real python API rather than just a thin peeling
back of a layer around "sys".
Totally agree with @cleeland , this is why we have added conan_api
to the codebase. It also feels cleaner internal design.
Is this still an on-going feature? I'm trying to import the create command and I'm having trouble with it.
from conans.client.command import create`
print ("Creating Conan package from source with channel {}".format(channelString))
create(self, ".", channel=channelString)
Fails with the error:
Traceback (most recent call last):
File "build_cereal.py", line 3, in
from conans.client.command import create
ModuleNotFoundError: No module named 'conans'
This is probably my problem but I've been having trouble finding any reference to conan_api on the web and my knowledge of python isn't great.
Extended attempt at usage here: https://github.com/luckielordie/cereal_conan
Yes, this seems to be mostly your python configuration. Make sure to add the conan folder to the environment variable PYTHONPATH before running your script.
Most helpful comment
Totally agree with @cleeland , this is why we have added
conan_api
to the codebase. It also feels cleaner internal design.