'Setting Up a Python Virtual Environment with Hydrogen in Atom

I'm in the middle of switching from VS Code to Atom and I'm trying to set up a virtual environment for my python project.

It was pretty easy to do in VS Code, I'd run the following script and it would automagically start using the new env (with all the required packages) when I'd run the script:

python3 -m venv my_env
source my_env/bin/activate
pip3 install -r requirements.txt

Now I'm trying to set up Hydrogen to work the same way. When I run lines of code inline with Hydrogen, I want them to be run in a virtual environment that has the imported modules I need from a requirements.txt file.

I was able to install the python3 kernel with the following commands:

python3 -m venv my_environment_name      # create a virtual environment
source my_environment_name/bin/activate  # activate the virtual environment
python -m pip install ipykernel          # install the python kernel (ipykernel) into the virtual environment
python -m ipykernel install   

And Atom is able to see it: Screenshot

However, I'm still puzzled as how to install my dependencies into the kernel. And if I do install my dependencies there, I don't want my next python projects to have all those modules in there. I'd love to have the fresh-slate that virtual environments promise.

Any help here would be appreciated. Has anyone had experience setting up a virtual environment that can be used by the Hydrogen package?



Solution 1:[1]

Ok, after some more experimentation, I was able to connect to a kernel that I had installed my requirements.txt into.

Here are the steps I took:

python3 -m venv env
source env/bin/activate
# make sure requirements.txt has ipykernel in it
pip3 install -r requirements.txt 
python3 -m ipykernel install --user --name=env

Then in Atom, press cmd-shift-p and find Hydrogen: Update Kernels.
Or manually Packages->Hydrogen->Update Kernels
After, I was able to use the kernel by doing cmd-shift-p again and selecting Hydrogen: Start Local Kernel and selecting env.

When I would run import statements via Hydrogen (selecting them and pressing cmd-enter), they would now know what to import! Horray!

Solution 2:[2]

Windows Version.
This is the windows version of the excellent answer by Tim Estes. Without the use of requirements.txt in case some folks such as myself would like to install ipykernel separately. (Thanks so much Tim, it saved me a ton of time)

python -m venv env
env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=env

Similar to Tim's answer, go ahead and update kernels at Atom.

Packages->Hydrogen->Update Kernels

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tushar Niras
Solution 2