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

How to configure emacs-jupyter on MacOS with Python 3 and venv? #257

Closed
lostman opened this issue May 8, 2020 · 11 comments
Closed

How to configure emacs-jupyter on MacOS with Python 3 and venv? #257

lostman opened this issue May 8, 2020 · 11 comments

Comments

@lostman
Copy link

lostman commented May 8, 2020

I have Python 3 installed using venv but when I attempt to run a jupyter-python code block on Org mode I see

Starting python3 kernel process...

Which is then followed by

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: no module named ipykernel_launcher

I can't figure out how to point Emacs or emacs-jupyter at the right Python version.

I'm new to Emacs and just trying it out for Org + Python. So pardon my ignorance!

@tavurth
Copy link

tavurth commented May 9, 2020

I've been using doom-emacs which comes with a layer in packages.el massively simplifying this process.

I think spacemacs should also have a layer.

If you are new to emacs, then Spacemacs would be a good recommendation.

If you choose to install doom-emacs, after installation of that system, go to ~/.doom.d/init.el and uncomment the line:

+jupyter        ; ipython/jupyter support for babel

Then run in a terminal:

~/.emacs.d/bin/doom sync

And restart emacs, to install all the packages and it should set everything up for you.

@tavurth
Copy link

tavurth commented May 9, 2020

If your problem is only the python2 rather than the python 3 then you could follow this tutorial:

https://stackoverflow.com/a/38806058

@lostman
Copy link
Author

lostman commented May 9, 2020

I have the same issue with spacemacs and doom-emacs.

Python 2 comes installed with Mac OS. Python 3 I have installed from Homebrew and I'm using it with direnv/venv.

For some reason emacs-jupyter defaults to Mac OS native Python 2, and I don't know how to change that.

ob-ipython picks up a correct Python 3 binary.

I launch Emacs from command line where my PATH is set up appropriately:

> echo $PATH
.../.direnv/python-3.7.7/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin

So the default python executable should be the one from direnv/venv.

> which python
.../s/.direnv/python-3.7.7/bin/python

So my question is: how does emacs-jupyter decide where to find python binary and how can I control it?

@tavurth
Copy link

tavurth commented May 9, 2020

Hmm, that's a bit weird as it should be working in that case:

echo $PATH
/usr/local/bin 
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin # Export to pdf from org mode
❯ which python
/usr/local/bin/python

Seems like emacs-jupyter might use the /usr/local/bin/python, can you run:

/usr/local/bin/python --version

@tavurth
Copy link

tavurth commented May 9, 2020

Relevant lines in emacs-jupyter where python is located.

Seems like it references jupyter --paths, perhaps you can show the output of that?

@lostman
Copy link
Author

lostman commented May 10, 2020

@tavurth Yes, emacs-jupyter is picking up /usr/local/bin/python.

In exec-path I can see:

"/usr/local/sbin/" "/usr/local/bin/" "/usr/bin/" "/bin/" "/usr/sbin/" "/sbin/" ".../.direnv/python-3.7.7/bin/"

which is at odds with my PATH:

.../.direnv/python-3.7.7/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin

where the venv python has priority.

As I've said, I'm new to Emacs and I'm not sure what to do about it. How can I configure Emacs or (jupyter-emacs) to prioritize a particular path?

@tavurth
Copy link

tavurth commented May 10, 2020

I'm not sure of the exact specifics behind getting emacs-jupyter to use a specific python version but as Python2 is now dead I would suggest simply changing your systemwide python -> python3.

You can do that as follows:

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3 /usr/local/bin/python

Sorry I can't be of more help with the specific venv python case.

Edit: This fixed my issue almost a year later 🤣

@dangom
Copy link
Contributor

dangom commented May 10, 2020

See exec-path-from-shell. for more info. You need your python (or jupyter) executable of choice to be in the PATH, and you need Emacs to know the path. There is no magic here.
Try running (getenv "PATH") to query what Emacs sees.

@lostman
Copy link
Author

lostman commented May 14, 2020

@tavurth I can't unlink the default python that is installed on Mac OS. It's located in /usr/bin/python.

@dangom I already have a suitable python executable in my PATH when I launch Emacs. And that PATH is propagated to Emacs. The problem is that emacs-jupyter is picking up /usr/bin/python. I am looking for a way to control this preference.

@koddo
Copy link

koddo commented Jun 24, 2020

@lostman, this solved the issue for me:

(setq org-babel-python-command "/Users/alex/.python_venv/default380/bin/python")

Update: Sorry, forgot to include the following:

$ pip install ipykernel
$ python -m ipykernel install --user

@dalanicolai
Copy link

I have a similar problem on Fedora 32. When I try to evaluate a jupyter-python source block, the echo area shows:
Starting python2 kernel process. And indeed the message buffer shows that it uses python2 to start a kernel.

However my system python is python 3. And when I evaluate a python org source block with:

import sys
print(sys.version)

It, correctly, tells me it uses python3. Which it should be as the value of my org-babel-python-command variable is 'python' (i.e. my default system python)
So I am wondering how to let jupyter-python use my default system python, just like normal python source block do.

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

5 participants