Using Hydrogen with Venvs: restarting Sys.Path


#1

Hi community,

I’ve made the switch to Atom as an IDE since I use R and Python and like the possibilities to run inline code through the Hydrogen package. I generally use venv’s in Python which I think is a bit difficult to manage together with Hydrogen (given that it relies on a python3 kernel that has to be present in advance). Right now I do the following to setup new venv’s and get my Hydrogen kernel to work in accordance:

  1. Make a new venv
  2. Activate the virtual environment through the atom-python-virtualenv package
  3. Pip install ipykernel, ipython and python-language-server
  4. Overwrite the path to the ipykernel used by Hydrogen to the python3 in my virtualenv

So far so good. Python is running from my venv, pip3 is working properly etc. However, my sys.path is still pointing at the python3 kernel used when I started Atom. I can thus far find no way in which I can reset the path to point towards the venv except manually changing it, which is annoying since it will require me to overwrite all previous paths with new ones everytime I switch environments. I’ve tried restarting the kernel and importlib.reload(site). Thus far, the only thing that works is following above steps, restarting Atom, and selecting the venv again and running code. I feel like there is a difference between Hydrogen’s ‘restart kernel’ option and actually restarting Atom.

Any ideas? Also, if my current approach is super tedious and can be done much easier: happy to completely change direction!


#2

What happens if you activate the venv from your command line and open Atom from inside it using atom .?


#3

I’ve tried the following three sequences:

activate venv_1 from the terminal and run atom .: correct venv, correct path when running !which python3 but sys.path remains the one which I last installed a new ipykernel for, let’s call that venv_0 (note: when I installed that ipykernel the previous time around, the sys.path would still be pointing to the previous ipykernel I had installed. Due to restarting Atom, the sys.path changed).

activate venv_2 from the terminal and run atom .: once again correct venv and which python3 path, but sys.path is still venv_0 (note that it didnt change eventhough opening venv_1 in the previous try: it seems to be fixed to the last python location where the ipykernel was initialised too).

activate atom . without a venv: once again, all seems good: global environment and /usr/local/lib/python3 path. Still sys.path pointing to venv_0.

For completeness: I assign a python version to Hydrogen’s ipykernel by running the following code:

!~/venv_test/bin/python3 -m ipykernel install --user

#4

Sorry, I don’t use Hydrogen and don’t know how it works well enough to know if there’s an easy answer. All of my virtualenv work has been done purely on the command line, not with Python code shelled out by a package to a third-party interpreter.


#5

No worries - thanks for taking the time!


#6

See: https://github.com/nteract/hydrogen/issues/243#issuecomment-203686955