Skip to content

Set default IPython profile with environment variable #11862

Open

Description

@lewisacidic

It would be nice to set the IPython profile with an environment variable, i.e. something like

$ IPYTHON_PROFILE=testing ipython
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.7.0 -- An enhanced Interactive Python. Type '?' for help.

IPython profile: testing
In [1]:

My particular use case is to use the conda {de,}activate.d/ hooks to change profiles based on which conda environment is active. At the moment, an easy hack is to alias ipython to ipython --profile={profile} and then unalias in deactivate, but environment variables would be cleaner.

Activity

  1. added this to the wishlist milestone on Aug 21, 2019
  2. farisachugthai commented on Aug 22, 2019

    @farisachugthai
    Contributor
  3. lewisacidic commented on Aug 23, 2019

    @lewisacidic
    Author

    $IPYTHONDIR allows you to redefine $HOME/.ipython (where the profiles are located) to somewhere else, rather than which profile is selected.

    You could have a hack where each profile sits in its own .ipython directory, but this is worse than the alias trick.

  4. voronind commented on Feb 5, 2020

    @voronind

    👍
    It would be nice to have this feature. Many applications let use IPython instead of standard shell, but we cannot specify profile when it invokes.

  5. sfalmo commented on Jan 26, 2023

    @sfalmo

    Sorry to bump this issue, but for anyone who has a similar problem, I think there is a clean solution without an additional environment variable.

    IPython automatically loads the default profile in the $IPYTHONDIR path if it exists. So for a certain environment env, one could create a directory, e.g. $HOME/.ipython/env , and initialize the default profile in there via ipython profile create default --ipython-dir $HOME/.ipython/env. There should now be $HOME/.ipython/env/profile_default, where you can customize your IPython settings.

    On activation of the environment, you just have to set the environment variable $IPYTHONDIR to $HOME/.ipython/env (this can be automated in conda) and every IPython process started from now on will automatically select the default profile therein.

  6. Kentzo commented on Jun 29, 2024

    @Kentzo

    Would be handy for per-venv profiles. Storing IPYTHONDIR inside venv is an inconvenience as the whole directory has to be nuked whenever python is updated.

    The way I'm doing it now, to preserve the ipython profile between re-creations of venvs:

    > ipython profile create <PROFILE>
    > mkdir <venv>/.ipython
    > ln -s <IPYTHONDIR>/<PROFILE> <venv>/.ipython/default
    > echo "import os.path; os.environ['IPYTHONDIR'] = os.path.expanduser('<venv>/.ipython')" > <venv>/lib/pythonX.Y/site-packages/ipython.pth

    With IPYTHON_PROFILE that could be simplified to:

    > echo "import os; os.environ['IPYTHON_PROFILE'] = <PROFILE>" > <venv>/lib/pythonX.Y/site-packages/ipython.pth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions