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

[question] Recipe depends on python package #15249

Closed
1 task
andrey-zherikov opened this issue Dec 11, 2023 · 2 comments
Closed
1 task

[question] Recipe depends on python package #15249

andrey-zherikov opened this issue Dec 11, 2023 · 2 comments
Assignees

Comments

@andrey-zherikov
Copy link

What is your question?

My recipe (package_type = "python-require") requires specific python package to be installed. Usually we do pip install ... to install it in our CI but I'd like to make it a dependency of the recipe. Note that python package is not available as conan package.
I looked at conan.tools.system but it doesn't contain pip tool. On another side, there is a way to call pip from python code like in example here:

 def system_requirements(self):
    import pip
    if hasattr(pip, "main"):
        pip.main(["install", "colorama"])
    else:
        from pip._internal import main
        main(['install', "colorama"])

What is recommended way to ensure that a python package required by conan recipe is installed?

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@memsharded memsharded self-assigned this Dec 11, 2023
@memsharded
Copy link
Member

Hi @andrey-zherikov

Thanks for your question.
The main problem with python packages (pip) is that there is no guarantee that they can be installed, or that they won't destroy or conflict the current Python environment:

  • Conan can be running with the Conan self-contained executable. That means there is not even a Python environment neither pip exists to install things
  • If conan is running in a Python environment, installing pip dependencies can conflict with Conan own dependencies. colorama, for example is already a Conan dependency, trying or forcing to install a different version than the Conan one can break Conan
  • Even if the pip packages do not conflict with the Conan dependencies, they can still very easily conflict with other Conan packages, or previous versions from the same package requiring other potentially different versions of that package, then breaking each other

So in general, the recommendation is not to use pip install to install Python dependencies. The alternative would be to install them together with Conan when Conan is installed, using a single requirements.txt.

There is some related work in #11601 to create virtualenvs on the fly, but that only applies for executable tools, not for Python libraries that you aim to use from the recipes (or python-requires recipes), because they cannot be imported from a different context than the current one. So still using a single requirements.txt to install all your python pip packages into the same env is the recommended approach.

@andrey-zherikov
Copy link
Author

Thank you for the answer. Feel free to close the issue.

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

2 participants