Replies: 2 comments 7 replies
-
My instinct would be to opt for a simpler solution use extras_require: setup(
...
extras_require={
"qip": ["qutip_qip"],
"control": ["qutip_control"],
"all": ["qutip_qip", "qutip_control"],
},
...
) The entry points solution will still require qutip_qip to be already be installed. The optional import then becomes just: import sys
try:
import qutip_qip
sys.modules["qutip.qip"] = qutip_qip
del qutip_qip
except ImportError:
pass Thoughts? |
Beta Was this translation helpful? Give feedback.
-
P.S. Regarding |
Beta Was this translation helpful? Give feedback.
-
Background
This is an attempt to set up
qutip_qip
to be used as an optional package for qutip when installed. It can also be generalized to other qutip family packages such ascontrol
,lattice
ortensorflow
. As there are a few downstream packages using those modules (especiallycontrol
), allowing this will make the upgrade to qutip 5.0 easier for them.Expected behaviour
qip
will be a subpackage forqutip
but it is not imported toqutip
in the top-level__init__.py
. This is one of the original goals: make qutip importation lighter.qutip
is installed, importingqutip.qip
will pop anImportError
with instruction for installingqutip_qip
. In principle, it also allows a completely different version ofqutip.qip
be implemented inqutip
.qutip_qip
is installed, importingqutip.qip
will automatically using thequtip_qip
library, with no difference in the user interface.qutip_qip
if some flag is given when installingqutip
. Haven't look into this yet.Implementation
Using
entry_point
, it turned out to be easier than I thought.In
qutip_qip
, add a parameter to the setup function:The "group id",
qutip_qip_plugins
, will be used inqutip
to identify this entry point group. The stringqutip_qip
is the module to be exported toqutip
, which can also be replaced by a submodule such asqutip_qip.compiler
.In qutip, write the following in
qutip/qip/__init__py
This will look for the entry point group id, load
qutip_qip
if it is found. Then we point the namequtip.qip
to the modulequtip_qip
insys.modules
.With this, everything works smoothly
All of them return/use modules pointing to
qutip_qip
Note
I'm a bit surprised that even
import qutip.qip as quqip
gives me a module pointing toqutip_qip
, because thesys.modules
is actually changed fromqutip.qip
toqutip_qip
during the importation. Then I found that it is explained here in the footnote. The import function does not use return, rather it looks the name up insys.modules
and gives that back. However, it is also mentioned that "This is implementation-specific behavior that is not guaranteed to work in other Python implementations." So maybe we need alternatives if we want to go beyond CPython.Move it to
qutip/__init__.py
may solve this sinceqip
will always be loaded when initializingqutip
. It is not a very bad option because it will only be loaded ifqutip_qip
is installed. But still this might not be an optimal choice.Beta Was this translation helpful? Give feedback.
All reactions