Setting up Hydrogen and Atom with Anaconda on Windows
Asked Answered
S

1

9

I would like to run python interactively in ATOM using the Hydrogen package. I am on Windows 10. I would like to be able to initiate an ATOM session by double-clicking the ATOM icon in my toolbar or double-clicking a .py file and not have to resort to initiating ATOM via the command line. I have installed Python 3.8 via the Anaconda distribution. I have used pip install to install the ipykernel.

I get a series of Traceback errors which seems to be common;

Traceback (most recent call last): File "C:\Users\BRB\anaconda3\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\BRB\anaconda3\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\BRB\anaconda3\lib\site-packages\ipykernel_launcher.py", line 15, in from ipykernel import kernelapp as app File "C:\Users\BRB\anaconda3\lib\site-packages\ipykernel__init__.py", line 2, in from .connect import File "C:\Users\BRB\anaconda3\lib\site-packages\ipykernel\connect.py", line 18, in import jupyter_client File "C:\Users\BRB\anaconda3\lib\site-packages\jupyter_client__init__.py", line 4, in from .connect import File "C:\Users\BRB\anaconda3\lib\site-packages\jupyter_client\connect.py", line 21, in import zmq File "C:\Users\BRB\anaconda3\lib\site-packages\zmq__init__.py", line 47, in from zmq import backend File "C:\Users\BRB\anaconda3\lib\site-packages\zmq\backend__init__.py", line 40, in reraise(*exc_info) File "C:\Users\BRB\anaconda3\lib\site-packages\zmq\utils\sixcerpt.py", line 34, in reraise raise value File "C:\Users\BRB\anaconda3\lib\site-packages\zmq\backend__init__.py", line 27, in _ns = select_backend(first) File "C:\Users\BRB\anaconda3\lib\site-packages\zmq\backend\select.py", line 28, in select_backend mod = import(name, fromlist=public_api) File "C:\Users\BRB\anaconda3\lib\site-packages\zmq\backend\cython__init__.py", line 6, in from . import (constants, error, message, context, ImportError: DLL load failed while importing error: The specified module could not be found.

Things I have tried:

  1. I first added the Python3 executable to the system PATH (against the advice of Anaconda). This does work if I initiate ATOM via the command line, but if I double-click the ATOM icon or .py file, I get the usual traceback errors.

This answer https://discuss.atom.io/t/solved-how-to-use-the-hydrogen-package/30190 says it was a problem with Atom on macOS machines on older versions of Atom but has since been corrected for macOS. The answer was 4 years ago, so I assume it has been corrected for Windows, but I cannot find a soltion.

  1. This answer Setting up Hydrogen and Atom with Anaconda managing python installation says to (A) run:

    conda activate myenv python -m ipykernel install --user

and (B) clean up the PATH.

I did do both of these things. Removing the Python3 executable from PATH was a step backwards. Now it doesn't work even from the command line. I also don't understand why this is even bad in the first place...

  1. This guy Atom: Setting up Hydrogen Launcher for use with Python 3.7 has the same problems. But no solution.

Can someone point me in the right direction?

I am relatively new so would prefer not to complicate things by setting up multiple virtual environments etc at this stage (I don't yet have a need and I want simple).

Schnapp answered 6/11, 2020 at 9:4 Comment(3)
did you manage to get it to work?Hildy
Not yet. I still start atom from the anaconda command prompt which is not ideal, but haven't returned to trying to find a solution. I do need to come back to this at some stage unless you have a solution???Schnapp
#59509836 the comments here mention that on windows and python 3.8 they had similar import issues - did you try a different python version? Are you using virtual environments ?Spleenwort
E
1

I use Atom + Hydrogen extensively. And it is works which whatever python version and if it uses Anaconda, Miniconda, or simply python. Please do not mess with the system environment PATH.

Maybe you are confused about which python executable is which. Or, is Atom.exe and Hydrogen using the same version, same path, of python executable?

Atom thingy

I am sure that Atom.exe uses the default python version that exists system-wide or a virtual environment. If you run Atom.exe without a virtual environment from the terminal, or simply double click the icon from the desktop, Atom.exe will use python available in the system.

But if you run Atom.exe from a virtual environment-activated terminal/cmd, then Atom.exe will use python that is available in the virtual environment.

Why does it matters? If you use an Atom.exe package, for example, python-import-magic, Atom.exe will ask you for a python package named isort. If you installed isort in a virtual environment named work, Atom.exe will always tell you that isort is not available, unless you start Atom.exe from the work virtual environment.

Then, should I start Atom.exe from a virtual environment-activated terminal? I think that is not a good idea.

Hydrogen thingy

It doesn't matter how many pythons are installed or virtual environments are created, Hydrogen will use installed/registered kernels of python executables. For example, I installed several python versions (Yup, sorry I don't use pipenv) that located in:

  • python37 in D:\python\python37\python.exe
  • python38 in D:\python\python38\python.exe

And then I want to use Hydrogen with a kernel that uses python37, I should install the kernel once with:

D:\python\python37\python.exe -m ipykernel install --name "python37_something"

or if I need python38

D:\python\python38\python.exe -m ipykernel install --name "python38_something"

or I just want to install a kernel with whatever python is available system-wide:

python.exe -m ipykernel install --name "python_for_the_win"

After that, the installed kernel will appear in the Hydrogen (or even Jupyter Notebook/Lab) kernel selection. Screenshot below.

But how if I use a virtual environment? Just switch to the virtual environment and install the kernel. For example, if I want to use a virtual environment named work with Hydrogen, I should activate the virtual environment and install the kernel once for the first time:

workon work
python -m ipykernel install --name "work_venv"

or if you are using conda:

conda activate work
python -m ipykernel install --name "work_env"

The work virtual environment will appear in Hydrogen kernel selection. Here is my installed kernels as an example:

enter image description here

The important thing is, you should name the installed ipykernel with a --name argument and then choose a more meaningful name, such as work, flask development or anything you like, but not python or python3. The --user argument depends on your needs.

Why can't I use 'python' or 'python3' as a kernel name?

If you insist to run:

python -m ipykernel install --name "python"

or

python -m ipykernel install --name "python3"

or even the instruction from official nteract page:

python -m ipykernel install

from the virtual environment or not, the kernel will be installed, but the location of the kernel will be different depending on the virtual environment or system python path. Please take a look at my screenshots.

enter image description here

The terminal in the back is without activating a virtual environment and the terminal in the front is using a virtual environment named work. When I run jupyter kernelspec list from both terminals, all the installed kernels are listed, and the file paths to them are the same on both terminals. Except for the python3, I do not know why, but it seems that python or python3 is a default name for ipykernels kernel and also when installing a kernel without a name.

And you cannot choose which python3 to run from Atom+Hydrogen without, MAYBE, running atom.exe from virtual environment activated terminal.

Bonus

You may notice that in my last screenshot above, 'domainopsy' and 'javascript' are installed in C:\Users\faruq directory, it seems because I passed --user when I installed the kernel, while 'gym' and 'work' are not. I think, 'domainopsy' and 'javascript' are not accessible if I log in with another username.

Please pardon my English, and this writing is based on my experience, so please correct me if I am wrong.

Exoergic answered 27/10, 2021 at 23:51 Comment(7)
Hi Sandi. Thank you very much for this. The bounty expires in 50 minutes and I won't have time to look at whether this solves my issue until tonight. I will mark as accepted for now so that you get the bounty before it disappears (not sure how that works). Grateful if I can trouble you with some follow-up questions in case I run into issues.Schnapp
When you say "I always use a virtual environment. So if I want to use a virtual environment named 'work' with Hydrogen, I should register it for the first time." So you only need to do this once if say you don't want to switch between virtual environments? And where do you type the subsequent lines, in your Anaconda command prompt? Do you need anything up the top of your .py script to direct Hydrogen to whichever environment/kernel you are using?Schnapp
Hi brb! I am still working, please let me reply later, in 2 hours I think.Exoergic
By the way, dont worry about the bouties. I also don't know what it is. I am here to help you.Exoergic
Hello again brb!! 1) So you only need to do this once if say you don't want to switch between virtual environments? Yes. And by the way, by switching between virtual environments, do you mean that you open terminal/cmd and change the environment to something, and run atom.exe from the terminal itself?Exoergic
2) And where do you type the subsequent lines, in your Anaconda command prompt? In the terminal/cmd, all of them. And it is doesn't matter which terminal is it, since as long as you execute "workon" and "conda activate", you will move to a virtual environment. So as long as you are in the desired virtual environment, you can install the ipykernel.Exoergic
3) Do you need anything up the top of your .py script to direct Hydrogen to whichever environment/kernel you are using? No. As long as I registered the kernel, the list of the installed kernels will appear. And as far as I can remember, the list will be available to Jupyter Notebook too! But the important thing is you must not use "python" as a kernel name. For example, if I run "python -m ipykernel install --name 'python3'" from two vritual environments, it will lead to a problem. See my updated answer.Exoergic

© 2022 - 2024 — McMap. All rights reserved.