Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conan Python API Use Without Conan File #6315

Closed
szegel opened this issue Jan 9, 2020 · 7 comments
Closed

Conan Python API Use Without Conan File #6315

szegel opened this issue Jan 9, 2020 · 7 comments
Assignees

Comments

@szegel
Copy link

szegel commented Jan 9, 2020

Hey All!

I am trying to write a python script using the Conan Python API. I am trying to have conan download a package from the server and install that package in a specified directory without requiring a conanfile.py.

I have this package on my Conan Server:

> conan search -r=my_remote
my-pkg/0.0.1@my-usr/release

I have been looking at https://github.com/conan-io/conan/blob/develop/conans/client/conan_api.py, but I have been unable to find any documentation on the Conan Python API.

Currently my script looks like this:

from conans.client.conan_api import Conan
conan, _, _ = Conan.factory()
conan.config_install("<git repo url>", False, 'git')
conan.install(name="my-pkg", version="0.0.1", user="my-usr", install_folder="tmp", channel="release")

This gives the the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File “<path>/python3.6/site-packages/conans/client/conan_api.py", line 76, in wrapper
    return f(*args, **kwargs)
  File “<path>/python3.6/site-packages/conans/client/conan_api.py", line 544, in install
    conanfile_path = _get_conanfile_path(path, cwd, py=None)
  File “<path>/python3.6/site-packages/conans/client/conan_api.py", line 129, in _get_conanfile_path
    raise ConanException("Conanfile not found at %s" % " or ".join(candidate_paths))
conans.errors.ConanException: Conanfile not found at <path to cwd> or <path to cwd>

Thanks for the help!

@szegel szegel changed the title [Question] Conan Python API Documentation for Use Without Conan File [Question] Conan Python API Use Without Conan File Jan 9, 2020
@szegel szegel changed the title [Question] Conan Python API Use Without Conan File Conan Python API Use Without Conan File Jan 9, 2020
@memsharded memsharded self-assigned this Jan 9, 2020
@memsharded
Copy link
Member

Hi @szegel

I am trying to write a python script using the Conan Python API. I am trying to have conan download a package from the server and install that package in a specified directory without requiring a conanfile.py.

First, a word of caution. The python api is not documented nor stable. It might change at any time and break your scripts.

Then, you need the install_reference() reference method, not the install() one. This will not only download that package, but also the transitive dependencies. And not all configurations, only the defined one (input profile or default profile, settings, options).

Finally, the api is a command line alternative, it is not a low level access to Conan internals. As such, it is not possible to install packages in any random directory, as Conan packages follow some conventions, and they are installed in the Conan cache (by default the userhome/.conan). You can change the cache location with CONAN_USER_HOME env-var, but that will still be a Conan cache with a given layout. What you can do is to use the -g deploy, the deploy generator, that is able to do a copy of the package from the cache to the current directory.

@szegel
Copy link
Author

szegel commented Jan 9, 2020

Thanks so much for your help! I got a working solution. Here is the code that lets me install and deploy a specific package (and its dependencies) without a Conanfile from the Python API. I understand that the Python API is unstable, and this can break at any time.

from conans.client.conan_api import Conan
from conans.model.ref import ConanFileReference
ref = ConanFileReference.loads("my-pkg/0.0.1@my-usr/release", validate=False)
conan.install_reference(ref, install_folder="tmp", generators=['deploy'])

@szegel szegel closed this as completed Jan 9, 2020
@memsharded
Copy link
Member

Cool! Thanks for following up.

We will probably stabilize the python api at some point, after Conan 2.0, but it will take some time. In any case, it is not that it changes that wildly that we will be breaking it at every release, so good luck :)

@ericriff
Copy link

ericriff commented Jul 3, 2021

Is the API still unstable?
We do a bunch of conan calls from python scripts, currently through subprocess.popen(). It would be nice to use this instead.
If it is still unstable, are there any plans to change this? Maybe Conan 2.0?

@memsharded
Copy link
Member

Is the API still unstable?

Yes, everything that is not documented in docs.conan.io, is not stable. Actually everything that is not documented shouldn't be used at all.

The python API is the exception, we know there are a bunch users using it so:

  • We don't plan to break it in Conan 1.X, and we will try our best to not break it.
  • It will change in Conan 2.0, the api change will be breaking. We will document the api soon in Conan 2.X (maybe 2.1, 2.2..), first as experimental, and declare it stable when feedback is enough.

@Tricksam
Copy link

Same question as my predecessors...Is the Conan 2.0 API meanwhile stable?
I found that it's still not well documented.
After playing around I struggle to get a Conan workflow running, especially the upload causing some headache.

@memsharded
Copy link
Member

Conan 2.0 API is not documented yet, but it is expected to become stable quickly, but this will be most likely after 2.1. You have quite detailed usage in the commands themselves, that would be a good start point to check the API.

After playing around I struggle to get a Conan workflow running, especially the upload causing some headache.

You mean with the API? If you want to ask specific questions about any API behavior, feel free to open new tickets for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants