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

Does codebraid work with Python Anaconda environments? Trouble plotting with python from jupyter notebook #54

Open
00krishna opened this issue May 30, 2022 · 7 comments

Comments

@00krishna
Copy link

00krishna commented May 30, 2022

Hello. I use Anaconda to create virtual python environments. I have jupyter notebooks and jupyterlab installed in my load virtual environment, as well as codebraid. But for some reason, I can't seem to generate plots with the jupyter kernel, though everything else for python seems to work. So the key question is, do I need to add some instruction to point to the Anaconda environment?

I just ran this codeblock in the attached file, and the error message says that Matplotlib is not found.

Plots

```{.python .cb-nb jupyter_kernel=python3 session=random example=true}
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 1001)
x_tick_values = np.linspace(0, 2*np.pi, 5)
x_tick_labels = ['0', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$']
plt.plot(x, np.cos(x), label=r'$\cos(x)$')
plt.plot(x, np.sin(x), label=r'$\sin(x)$')
plt.xticks(x_tick_values, x_tick_labels)
plt.legend(prop={'size': 12})
plt.grid()

I know that matplotlib is installed, because I can open and ipython terminal session, and then import matplotlib.pyplot as plt and that works.
julia_output_python.pdf
julia-braid-test-python.md

The full error message is below.

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      3 import numpy as np

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2294, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2292     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2293 with self.builtin_trap:
-> 2294     result = fn(*args, **kwargs)
   2295 return result

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/magics/pylab.py:99, in PylabMagics.matplotlib(self, line)
     97     print("Available matplotlib backends: %s" % backends_list)
     98 else:
---> 99     gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100     self._show_matplotlib_backend(args.gui, backend)

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3444, in InteractiveShell.enable_matplotlib(self, gui)
   3423 def enable_matplotlib(self, gui=None):
   3424     """Enable interactive matplotlib and inline figure support.
   3425 
   3426     This takes the following steps:
   (...)
   3442         display figures inline.
   3443     """
-> 3444     from matplotlib_inline.backend_inline import configure_inline_support
   3446     from IPython.core import pylabtools as pt
   3447     gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:6, in <module>
      1 """A matplotlib backend for publishing figures via display_data"""
      3 # Copyright (c) IPython Development Team.
      4 # Distributed under the terms of the BSD 3-Clause License.
----> 6 import matplotlib
      7 from matplotlib.backends.backend_agg import (  # noqa
      8     new_figure_manager,
      9     FigureCanvasAgg,
     10     new_figure_manager_given_figure,
     11 )
     12 from matplotlib import colors

ModuleNotFoundError: No module named 'matplotlib'
@00krishna 00krishna changed the title Does codebraid work with Anaconda environments? Trouble plotting from jupyter notebook Does codebraid work with Python Anaconda environments? Trouble plotting with python from jupyter notebook May 30, 2022
@gpoore
Copy link
Owner

gpoore commented May 30, 2022

How are you running codebraid? Have you tried using a terminal in which the environment is activated?

@00krishna
Copy link
Author

@gpoore Yep, I ran this in the terminal with the Conda environment activated. And that Anaconda environment also has jupyter installed, as well as notebook. I even installed matplotlib in the base Conda environment, but that did not work either.

I used the command

codebraid pandoc -f markdown -t markdown --overwrite --standalone -o julia_output_python.md julia-braid-test-python.cbmd

I can see that the error messages refer to a path like: File /media/hayagriva/miniforge3/lib/python3.9/site-packages/matplotlib_inline. I think that this should probably be more like File /media/hayagriva/miniforge3/envs/<MYENVIRONMENT>/lib/python3.9/site-packages/matplotlib_inline , though the matplotlib library is in that media/hayagriva/miniforge3/lib/python3.9/site-packages/matplotlib_inline as well.

So something seems a bit off. I will keep playing with it. But let me know if you think there is anything missing.

@00krishna
Copy link
Author

Okay, I think I figured it out. It is a little screwy.

So if I write it this way,

```{.python .cb-nb jupyter_kernel=python3 session=random example=true}

then I get the problem.

BUT, if I write it this way, then no problem.

```{.python .cb-nb jupyter_kernel=python3 example=true}

So basically I should not names the session in the first codeblock where I execute or create the jupyter_kernel? So it seems to work now, but not sure if this is an intended error.

@gpoore
Copy link
Owner

gpoore commented May 30, 2022

That is strange. session should have no effect. However, you also don't really need named sessions unless you are splitting your code into independent pieces that you want to run separately in different kernel instances.

@00krishna
Copy link
Author

Sure, that makes sense. Would you like me to close the issue or keep it open--in case you want to track this behavior where session causes the error message. As for me, I should probably be okay. But people who use multiple sessions might run into this error?
Let me know what you prefer.

@gpoore
Copy link
Owner

gpoore commented May 30, 2022

Let's leave the issue open for a few days in case you run into additional problems with this, and so I can try testing a few things. session is just a string used for internal bookkeeping and naming things in the cache, so it would be good to determine what's happening here.

@erooke
Copy link

erooke commented Aug 17, 2022

This might be related to #57. Changing (or removing the session) causes the cache to not be hit and re-runs the code. If there was an attempt to run codebraid before installing the desired package you would see this behavior. That's how I triggered this anyway.

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

No branches or pull requests

3 participants