myQLM / myqlm-issues

4 stars 1 forks source link

The command `python -m qat.magics.install` writes global config #20

Closed NathanCummings closed 1 year ago

NathanCummings commented 2 years ago

Not exactly a bug as it appears to be somewhat intended behaviour, but by writing the ~/.ipython/profile_default/ipython_config.py file, it causes ipython to try and call the qat module anytime ipython is used.

Since I installed myqlm in a virtual environment, ipython will only now only start without throwing errors when I have that virtual environment activated.

If I delete ~/.ipython/profile_default/ipython_config.py, I then need to rerun python -m qat.magics.install whenever I return to that project.

Could this be solved another way?

Many thanks

ArnaudAtos commented 2 years ago

Dear @NathanCummings Thank you for having reported this limitation.

IPython always consider the global directory ~/.ipython (if IPYTHONDIR environment variable is not defined), even if you are in a Python virtual environment.

This limitation can be bypassed, by creating a script that register this magic (and would fail silently is you are not in a virtual env). Every python file present under ~/.ipython/profile_default/startup/ is executed by IPython at start-up. Then, you can create a file named ~/.ipython/profile_default/startup/myqlm.py containing the following code:

# -*- coding: utf-8 -*-
"""
File ~/.ipython/profile_default/startup/myqlm.py

This file register myQLM magics (or fails silently if not in virtual env)
"""

try:
    from IPython import get_ipython
    from qat.core.magic import load_ipython_extension

    load_ipython_extension(get_ipython())

except ImportError:
    # myQLM can be installed in a virtualenv
    pass

PS: The command python -m qat.magics.install will be updated for the next release, to consider your limitation

EDIT: File ~/.ipython/profile_default/ipython_config.py must be deleted (or updated to not point to qat module)