millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

how to setup virtualenv #793

Closed HumHongeKamyaab closed 3 years ago

HumHongeKamyaab commented 3 years ago

I want to thank you for this great package.

I was looking for variables that can be set using .dir-locals.el to use python virtualenv when opening a Jupyter-notebook file in a project. Its the only feature preventing me from migrating to EIN.

Can someone help me in this regard.

while using Jupyter notebook, I use following workflow to activate virtualenv, assuming ~/pythevirons/fooenv/ is directory of virtualenv,

dickmao commented 3 years ago

EIN is just a client. Your virtualenv inquiry is prima facie entirely on the server side. I suspect you can google the answer to your question better than I can since virtualenv perennially confuses me.

Also, running the server as jupyter lab won't work. Commit 142ff50 should now warn people of this.

HumHongeKamyaab commented 3 years ago

Hi, Thanks for the reply. I got your point. I just need to specify the location of jupyter server situated at following location ~/pythevirons/fooenv/bin/jupyter in my system.

I tried to customize ein:jupyter-server-command to the custom command (~/pythevirons/fooenv/bin/jupyter) for this purpose, but its not working.

Is there a EIN variable where we can specify the location of jupyter server installed on the system,

dickmao commented 3 years ago

but its [sic] not working

It's hard to diagnose the problem from general statements.

One quick diagnostic is M-: (executable-find ein:jupyter-server-command)

HumHongeKamyaab commented 3 years ago

Thanks, I managed to solve it finally.

For people, who want to use EIN with python virtualenv specific to their project can specify following .dir-locals.el in their project root,

(nil . (
        (ein:jupyter-server-command . "~/pythevirons/fooenv/bin/jupyter")
        ))

where, ~/pythevirons/fooenv is path to the virtual env.

The above command will force EIN to use jupyter server installed at ~/pythevirons/fooenv/bin/jupyter in your machine.

Alf0nso commented 3 years ago

I would like to share two bits of knowledge on this topic, first is this Reddit Comment helped me have this working:

image

And well.. the second bit is the function ein:notebook-switch-kernel which lets you switch kernels (note: you need to have done what was metioned on the comment above in order to have kernels to switch to).

image

With this two things I managed to make EIN work with pyvenv flawlessly, at least until now :)

zmberber commented 3 years ago

If I do what the last comment by Alf0nso suggests, then if you select that kernel, new notebooks will have the metadata changed to that kernel with the hard path. that caused some problems for when i shared the project with other collaborators on the project (it doesn't work because they don't have the same path as i have or they may not even be using any virtual environment etc). what works (at least it seemed to work for me) is to create new notebooks using the default kernel Python 3 and then doing the ein:notebook-switch-kernel command everytime i open that notebook. that is very annoying, so i don't like that.

what HumHongeKamyaab suggests didn't work for me. it starts the server with the normal jupyter command, and not the one in my venv that i specified in .dir-locals.el as was recommended. does anyone have an idea what could be going wrong?

dickmao commented 3 years ago

Wouldn't it be easiest just to source myenv/bin/activate, then run emacs from your virtualenv? Having to get .dir-locals.el just right and then having to remember to switch the kernel, how do you guys get any actual work done?

zmberber commented 3 years ago

yes, that works, thanks!

in my emacs workflow, and in that of very many, the emacs server is used, and i just use emacsclient to connect to it. i would have to start a new instance of emacs everytime i work in a specific virtual environment, and i don't like that workflow.

zmberber commented 3 years ago

ok i got the .dir-locals.el to work. it reads the following.

((nil . ((ein:jupyter-server-command . "/path/to/venv-directory/bin/jupyter"))))
RAraghavarora commented 10 months ago

@HumHongeKamyaab @zmberber Thanks, that helped me. IMO this should be mentioned in the documentation. Having virtualenv support is a must

Alf0nso commented 10 months ago

I agree with this, the documentation should definitely have a section addressing this, since virtual environments is quite a big thing in python (I at least never work on python outside of a virtual environment"