Better Python code reuse
See original GitHub issueCurrently, it is possible to reuse Python code by requiring it and tools.pythonpath
. This is a bit limited because it’s not possible to import the module before the ConanFile is loaded, which makes it impossible to do some things (such as subclassing ConanFile). I tried with a profile as well:
[build_requires]
mypymodule/1.2.3@sztomi/stable
… but this doesn’t work either. Maybe it would make sense for build_requires
in profiles to be parsed before the conanfile is loaded. Or a separate, special section just for python deps of the conanfile. Is there a better way? What are your thoughts?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:50 (37 by maintainers)
Top Results From Across the Web
4. Code Reuse: Functions and Modules - Head First Python ...
And when it comes to reusing code in Python, it all starts and ends with the humble function. Take some lines of code,...
Read more >Python Function: The Basics Of Code Reuse
A Python function can be defined once and used many times. So it aids in code reuse: you don't want to write the...
Read more >How to Create, Import, and Reuse Your Own Module in Python
Where Does Code Reusability Work? ... Modularizing and reusing your code is a best practice for any project you're running. If you write...
Read more >How to Effectively Reuse Your Old Code in a New Project
Steps to creating your personal package · Create an empty file called _init.py_ . This file is used to mark directories on disk...
Read more >Best practice for reusing python code [closed] - Stack Overflow
Copy and paste. This is far away from best practice. · Add the folder of the library app to the environment variable PYTHONPATH:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This has been implemented in https://github.com/conan-io/conan/pull/3337, and will be then available (as experimental feature) in conan 1.7. Please stay tuned for the release, and looking forward your testing and feedback.
@memsharded, I think you misunderstood the functionality of my proposed
tools.require()
. It does not (and must not) depend at all of the conanfile currently being parsed, since that’s the limitation @sztomi was talking about. The idea is to be able to get parsing dependencies before requiring parsing, so it must not appear insideConanFile
class - and I suggest using pure Python (i.e., a function call from the always-availableconans
) instead of another file to specifypy_requires
.I believe this idea is semantically correct, since these dependencies are not requirements of the package, they’re requirements for the conanfile of the package, and thus they should not be controlled by the contents of
ConanFile
class, should not be visible or controlled anyway from downstream and they should not view or control any of the upstream requirements of the package (it may do so for the requirements of the parsing requirement it is importing, but that seems overkill for now).Regarding implementation, it is indeed a complex operation, but the operation is already ready to be used: it is just have to download the package using Conan (if not already in cache) and update
sys.path
. I believe it is safe to assume the package being required is pure-python and therefore not requiring a build step or anything else, but on the other hand, a full-featuredconan install
running under the hood might be as easy to implement as just fetching packages. When user callstools.require("mypymodule/1.2.3@sztomi/stable")
, it should:conan install mypymodule/1.2.3@sztomi/stable
PYTHONPATH
from thepackage_info
of the installed packagesys.path
or return a context manager liketools.pythonpath
doesOptions and settings, if that makes any sense for a Python requirement (C extension modules?), may be specified as extra arguments to
tools.require
. One little extra care must be taken regarding settings, since it does not make sense to use the same that are being injected to the package: suppose I’m cross-compiling some C package and itsconanfile.py
requires a Python package with a C extension, I want to build that extension to run in my machine, not on the package’s target. To solve this, my suggestion is to simplify: for a first version oftools.require
do not make options available and forbid installing packages that specify any settings. This is OK for all real use cases - we are only limiting the way one can specify how to build extension modules for reusable conanfiles. Or perhaps disable building at all in this case.Thanks