A development environment for writing Python code including Python notebooks. If you are new to python and juptyerlab, there is a self-paced introduction which can be found here.
In Jupyter, before you can successfully run the notebook, you’ll need to select the Jupyter kernel for this project. If it doesn’t appear in the drop-down list, run this in a terminal:
. myproject/venv/bin/activate python3 -m ipykernel install --user --name="venv_PROJECTNAMEHERE" --display-name="My project (Python3)"
And if your project has analytical scripts that run in a terminal you could add:
To run the python scripts, you’ll need to activate the virtual env first:
cd myproject . venv/bin/activate python3 myscript.py
Using a virtual environment in Jupyter
It is advisable to use a different virtual environment (venv) for each project you do in Python. There is a little bit of set up to get Jupyter working with a venv. Follow the instructions below to get started:
If you haven’t yet created a virtual environment for your project, in terminal run:
cd myproject python3 -m venv venv
In the terminal, inside your project directory, activate your venv:
Install the module ipykernel within this venv (for creating/managing kernels for ipython which is what Jupyter sits on top of):
pip install ipykernel
Create a Jupyter kernel which is configured to use your venv. (Change the display name to match your project name):
python3 -m ipykernel install --user --name="venv_PROJECTNAMEHERE" --display-name="My project (Python3)"
In Jupyter, open your notebook and then select this new kernel by its pretty name in the top right hand corner. It might take a little time/refreshes for it to show up.
To resume work on this after working on another project:
Activate the environment:
cd myproject source venv/bin/activate
Now you’ve activated this terminal with your venv, things you run on the command-line will default to using your venv for python packages, rather than the system’s packages. That’s useful if you run ‘python3’, run python scripts or ‘pip install’ more packages.
Open the notebook - it’s remembered which kernel you wanted to use for this notebook and you can carry on working with the packages available.
Note: Once you have associated the kernel with the venv you dont need to recreate/update it. Any packages that are installed to the venv via pip after the kernel is established are immediately available to the kernel.
Using pipenv in Jupyter
pipenv is another environment manager for Python. In general, please refer to their basic guidance.
Set-up for a project results in the creation of
Pipfile.lock in the root directory of your project folder.
The instructions for someone to install the packages specified in Pipefile/Pipefile.lock, are as follows (you don’t create a venv yourself, nor is it necessary to ‘activate’ the pipenv environment):
cd myproject pipenv install
To use the pipenv in Jupyter, compared to using a venv in Jupyter, the syntax of creating the kernel is simply adjusted to:
pipenv install ipykernel python3 -m ipykernel install --user --name="pipenv-name" --display-name="My project (Python3)"
And then select the kernel in Jupyter as normal.