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

passing environment variables to the kernel #359

Closed
keewis opened this issue Dec 3, 2019 · 5 comments · Fixed by #362
Closed

passing environment variables to the kernel #359

keewis opened this issue Dec 3, 2019 · 5 comments · Fixed by #362

Comments

@keewis
Copy link
Contributor

keewis commented Dec 3, 2019

xref pydata/xarray#3567

I'm trying to pass PYTHONPATH from conf.py to the kernels in order to make sure the notebook is executed using the source checkout instead of a version pulled by dependencies.

I tried to modify $PYTHONPATH (as recommended in the related #96) by editing os.environ in conf.py, which seems to work:

root = pathlib.Path(__file__).absolute().parent.parent
sys.path.insert(0, str(root))
os.environ["PYTHONPATH"] = str(root)

is this something I can rely on? If so, maybe you should add that to your documentation?

@mgeier
Copy link
Member

mgeier commented Dec 5, 2019

TBH, I don't know.

nbsphinx is simply calling nbconvert.preprocessors.ExecutePreprocessor in whatever context Sphinx extensions (source parsers, to be more specific) are running.

I don't know whether ExecutePreprocessor provides a way to explicitly provide environment variables. Or whether there are any guarantees regarding os.environ.
You would have to ask the nbconvert team about that.

There is a related option https://nbsphinx.readthedocs.io/en/latest/usage.html#nbsphinx_execute_arguments, but I guess this doesn't help in your case?

@keewis
Copy link
Contributor Author

keewis commented Dec 5, 2019

I looked into ExecutePreprocessor, which uses jupyter_client.KernelManager to start the kernels. KernelManager.start_kernel passes the contents of os.environ to the kernel (after removing PYTHONEXECUTABLE): this is how the kernels receive the environment variables. So I guess unless the source parser context changes os.environ that's a yes?

@mgeier
Copy link
Member

mgeier commented Dec 6, 2019

I guess so.

It's really hard to give any guarantees, because there are so many moving parts.

But I'd say it seems to be currently a reasonable way to achieve what you want.
Whether it will work in the future, nobody knows.

If so, maybe you should add that to your documentation?

You should add that to the documentation!

There is already a section "Controlling Notebook Execution" where this would fit.

@keewis
Copy link
Contributor Author

keewis commented Dec 6, 2019

I'm trying to figure out where to add this: would that be a new subsection (i.e. linking to a new notebook in executing-notebooks.ipynb)?

@mgeier
Copy link
Member

mgeier commented Dec 6, 2019

Yes, I think a new notebook would be nice for that.

This new notebook could probably also mention nbsphinx_execute_arguments (or not?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants