DEV Community

Joe Neville
Joe Neville

Posted on • Updated on

JupyterLab in a virtual environment

Why?

🏋️‍♀️ When it comes to running JupyterLab & JupyterNotebooks, the install is pretty hefty, with quite a few dependencies.
🦉 To avoid the travails of picking through a messy base install of Python on your dev machine, it is wise to always use a virtual environment.
👍 Thus running JupyterLab in a virtual environment is a good idea.

How?

Here's how using macOS.

  1. Navigate to a suitable directory in which want to work, or make a new one.
  2. Copy the Jupyter Notebook that you want to work on to this directory.
  3. Create a virtual environment and give it a name, in the example below, I'm using 'jup1'.

    python3 -m venv jup1
    
  4. Activate the the virtual environment.

    source jup1/bin/activate
    
  5. Now that the venv is active we can install JupyterLab.

    pip install jupyterlab
    
  6. Start JupyterLab.

    jupyter lab
    

Extra Tips

  • The shortcuts for Jupyter Notebooks really help to speed up working with them. It pays to invest some time in learning them. Or, if you can't be bothered with that, just remember that 'shift' + 'return' runs a cell.
  • If you want to check your notebooks into version control, such as github, make sure you clear the outputs first. To do so right-click on the notebook, or Edit > Clear All Outputs. Then save and do your commits.

Top comments (5)

Collapse
 
erikvangameren profile image
Erik van Gameren

Hi Joe,
Well, since two months (update Mac OS?) my Jupyter Labs doesn't work. It gives “ModuleNotFoundError”. A collegue tipped your blogpost, but that doesn't work for me, unfortunately. I made a virtual environment, installed Jupyter Lab and started, but got the same error.
I hope you can help me further.

Greetings, Erik

Collapse
 
joeneville_ profile image
Joe Neville

Hi, I'm running macOS 10.15.7.
I've just run through the commands as posted above and it works for me. Not particularly helpful for you, I understand.
“ModuleNotFoundError” looks like a Python error, it should have an additional message that tells you which module is missing.

Collapse
 
erikvangameren profile image
Erik van Gameren

And, if I have followed your instructions above and have installed Jupyter Lab, how do I handle the second time when I open Terminal?

Collapse
 
erikvangameren profile image
Erik van Gameren

Well, all the modules are missing, it seems that python looks in the wrong directory?

Thread Thread
 
joeneville_ profile image
Joe Neville

Sounds like you have not activated the virtual environment, if you had, then there would be a local python interpreter and the required dependencies i.e. those modules.
When you run this a second time, you need to start at step 4 and activate the virtual environment again.