pythonipythonatom-editorcondahydrogen

How can I run atom and hydrogen within a conda env using bash script?


I used to have a simple bash script that I was using to activate a conda env and then run atom. Then, I could run python code using hydrogen which could automatically see the packages inside myenv.

The previous bash scrip was sth like this:

#!/bin/bash

source activate myenv && 
atom

Since conda > 4.4 that 'source activate' does not exist anymore, I had to revise the script to:

#!/bin/bash

source /home/ubuntu/miniconda3/etc/profile.d/conda.sh &&
conda activate myenv &&
atom

However, hydrogen does not detect myenv anymore and runs the code from the base env which results into error due to packages missing from the base env.

When I replace atom with spyder, the above script works fine and spyder kernel does see myenv.

Any idea how I can get this fixed?

Update 1:

I did some more debugging. It seems that my ipykernel is not assigned to the kernel installed within the activated environment but assigned to the default ipykernel.

When I try jupyter kernelspec list in my activate env, I get:

python3    /home/ubuntu/.local/share/jupyter/kernels/python3

But on another system, I get

/home/ubuntu/miniconda3/envs/myenv/share/jupyter/kernels/python3

which seems to be the right kernel.

Any idea on how to fix this?

Update 2:

It seems that this resolves the issue in Update 1. Then, I was able to use the accepted answer to load atom within the desired env and have hydrogen run from the ipykernel in that env.


Solution

  • source activate still works as before, although it is no longer the preferred way to activate an environment.

    If you're only running a single command, try

    conda run -n <envname> <command>
    

    possibly with sourcing the profile first.

    I haven't tried, but a potential problem is that you're using && after sourcing the profile. I assume that the line continuation characters (\) got lost there when you wrote the question. Just write the commands into separate lines, so that the profile is loaded before bash processes the next command.