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

Gallery examples that use altair.expr module #657

Closed
mroswell opened this issue Mar 26, 2018 · 21 comments
Closed

Gallery examples that use altair.expr module #657

mroswell opened this issue Mar 26, 2018 · 21 comments

Comments

@mroswell
Copy link
Contributor

mroswell commented Mar 26, 2018

I see the "Decision: remove expr module" in #459

But many of the examples in the gallery include
from altair.expr import datum

So each of those examples yields a ModuleNotFoundError

For instance:

Hopefully these examples can be updated so that they function.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

Sorry for the confusion – we removed the old bloated expr module before adding back in a new version of the expr module. It will stay, and those examples will continue to function.

@jakevdp jakevdp closed this as completed Mar 26, 2018
@mroswell
Copy link
Contributor Author

perhaps I have to do the 'run from master' (python setup.py install) technique? My 2.0.0rc1 install via conda install altair jupyterlab --channel conda-forge doesn't seem to include the updated code

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

It depends what you want to do... if you install 2.0.0rc1 then you should have the expr module installed as well.

Try running

import altair as alt
print(alt.__version__)
print(alt.__path__)

to make certain you're importing the version you think you're importing.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

Oh, you can't install 2.0.0rc1 via conda-forge... you need to install it via pip.

If you did conda install altair, then you have altair version 1.3.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

See the installation page for details: https://altair-viz.github.io/getting_started/installation.html

@mroswell
Copy link
Contributor Author

mroswell commented Mar 26, 2018

For version and path, I get:

2.0.0dev
['/usr/local/lib/python3.6/site-packages/altair-2.0.0.dev0-py3.6.egg/altair']

and when I do !pip freeze | altair
I get

altair==2.0.0rc1

@mroswell
Copy link
Contributor Author

screen shot 2018-03-26 at 11 56 28 am

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

That's very strange... were these cells run with the same kernel? I see that the top cell has execution count 4 and the bottom cell has execution count 1. Can you restart the kernel and then run these in sequence?

@mroswell
Copy link
Contributor Author

same result after restarting the kernel screen shot 2018-03-26 at 12 57 17 pm

I also did a conda uninstall altair, just in case that was a factor

The execution count was likely because I collapsed the !pip freeze into one cell so it would fit in the screenshot

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 26, 2018

OK, so you're not actually running 2.0.0rc1... the !pip freeze is not associated with the kernel you are using in your jupyterlab session. In Jupyter, you're using 2.0.0dev, and I suspect your 2.0.0dev install is from a while ago if altair.expr is not available.

So basically, you pip installed Altair 2.0.0rc1 in one Python kernel, and you're running Jupyterlab on another kernel, so you're importing an old version of Altair.

This is one of the biggest warts of the jupyter multi-kernel model right now. I suspect if you run

!which python

and

import sys; print(sys.executable)

you will see different executables.

I wrote a whole blog post about this issue; you can read it here: https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/

What you need to do is make certain you have installed altair in the kernel you're using in JupyterLab. The easiest way to do this is probably to exit Jupyterlab, run the pip install command, open JupyterLab, and open a new notebook with the default Python 3 kernel, which matches the executable used to start JupyterLab.

Let me know how it goes.

@mroswell
Copy link
Contributor Author

Indeed, they were two different executables
One was
/Users/marjorieroswell/anaconda2/envs/py36/bin/python
and the other was the regular python36 path

I've tried a slew of things, and still no luck..

I'm running a conda environment, that in the terminal has pip installed altair. But now that both are consistently using the conda36 environment, I'm getting ImportError: No module named altair even though in the terminal, it shows I've got altair==2.0.0rc1

I'm going to try to use regular virtualenv to get a python3 environment, to see if I have any better luck that way.

actually, before I do that, I'll see if I can work my way through your blog post.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

Are you using Conda environments, or just the root environment?

@mroswell
Copy link
Contributor Author

mroswell commented Mar 27, 2018 via email

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

Ah, I know what's happening then. I suspect you're creating a conda environment, installing Altair in it, and not installing JupyterLab. This means that when you run jupyter lab it's running it in a different environment than your Altair installation.

So you need to make an environment:

$ conda create -n altair-test python=3.6
$ source activate altair-test

and then follow the installation instructions from https://altair-viz.github.io/getting_started/installation.html#installing-altair-with-jupyterlab including installing JupyterLab (don't skip that step)

$ pip install altair==2.0.0rc1
$ pip install jupyterlab
$ jupyter labextension install @jupyterlab/vega3-extension

Now you can run jupyter lab, then open a new notebook with the "Python 3" kernel, and then plots should work.

If you forget to install jupyterlab in the new environment, then launching juptyerlab will use the executable from the root environment, and plots won't work.

If you open a notebook with the wrong kernel, then it won't have access to the altair version that you installed, and plots won't work.

Hope that helps

@mroswell
Copy link
Contributor Author

To the helpful instructions you provided above, I added:
python -m ipykernel install --name altair-test
before I ran jupyter lab
and switched to the altair-test kernel

Then after overcoming all the installation challenges, I had to remember to actually print the chart in the gallery examples!

It might be helpful if each gallery example ended with
chart
so that each chart prints by default with a simple copy-paste. (Would that work, or would it somehow mess up the appearance of the chart in the documentation?)

A note that the guidance here:
https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/
seems to behave differently.

For instance, success didn't require me to do this in the Jupyter lab
!{sys.executable} -m pip install altair==2.0.0rc1

I just did
pip install altair==2.0.0rc1
at the terminal

Given this tweet, and that others are encountering a range of similar issues:
https://twitter.com/akarve/status/978442506118471680
it would probably be helpful if someone provided a video of things that can go wrong, and how to fix 'em...

In any case, the altair.expr import are now working for me. (I'm not yet confident that I can talk others through it.)

I think part of the challenge is that I don't exactly understand what a Jupyter lab/notebook kernel is. Maybe in addition to the if-this-goes-wrong-then-do-that, it might help to explain why

import sys; 
print(sys.executable)

and

!which python

yield different results, and how that relates to executables and Jupyter Lab kernels.

In essence:

  • It's working for me (yay!) but my guess is that others will need to be talked through similar issues... Some video content to assist might help.
  • Does the addition of python -m ipykernel install --name altair-test to the instructions seem to make sense (along with switching to the newly-added kernel)? (I think that's what worked for me.)
  • I gather !{sys.executable} -m pip install altair==2.0.0rc1 is not necessary to be run inside the Jupyter lab?
  • Can chart be added to the end of all of the gallery samples?

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

Given this tweet, and that others are encountering a range of similar issues:
https://twitter.com/akarve/status/978442506118471680
it would probably be helpful if someone provided a video of things that can go wrong, and how to fix 'em...

That's why I wrote that blog post... note that the blog post is not an attempt to help people install Altair, but an attempt to help people understand why pip install / conda install doesn't always put things in the place you might naively expect them to. More of a "here's the background you need to know in order to solve these problems yourself" kind of thing. Though I admit I probably could have done a better job at it.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

Does the addition of python -m ipykernel install --name altair-test to the instructions seem to make sense (along with switching to the newly-added kernel)? (I think that's what worked for me.)

No, I think this adds too much confusion, because it's unnecessary. You just need to choose the Python 3 kernel (which in this case is now identical to the altair-test kernel) This is why in the installation instructions I emphasize "Choose the Python 3 kernel". As long as you follow the installation instructions exactly, it will work.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

I gather !{sys.executable} -m pip install altair==2.0.0rc1 is not necessary to be run inside the Jupyter lab?

No, the only thing that is necessary is to follow the exact installation instructions given in the docs. That pattern from the (unrelated) blog post is what you need if you want to install a package within an already running jupyter notebook, which is orthogonal to the altair installation question.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

Can chart be added to the end of all of the gallery samples?

That could be helpful.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

I think the best option is to point people to Colab, now that it's working. There are just too many variables in play with JupyterLab/Jupyter notebook + jupyter extensions/ipyvega + conda envs/pip envs + exact versions needed + confusion about installation and execution paths.

@mroswell
Copy link
Contributor Author

Good to know about Colab!!
https://research.google.com/colaboratory

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

2 participants