Python and Jupyter¶
Python is an easy to write general purpose programming language, meaning it can be used for a variety of different applications.
Jupyter Notebooks are a great way to combine code and its output with documentary text and images all in one document. It’s also great for developing code, as you can execute blocks of code and see/ visualize the result inside the notebook.
(The material for this seminar has the form of a JupyterBook. It consists of Jupyter Notebooks and Markdown-Files.)
It is recommended to create a virtual environment for your project/ setup. All the libraries and dependencies installed for this project are isolated and won’t conflict other libraries and their dependencies. The easiest way to create a virtual environment may be with conda, but other options like pipenv are also possible.
Download and install Anaconda. Next to Python, a GUI (Anaconda Navigator) to manage packages and environments is included.
Download Miniconda3 for your OS and execute the installer. Miniconda includes Python.
Open the command line of your operating system (see help for Linux, Mac, (Windows) (for Windows users: open “Anaconda Prompt”) and create a new environment. Type the following line by line and press
Enter after each line.
# check if conda is installed conda --version
Create a new environment¶
Change the name
my_environment in the code below to a name of your choice. This is the name of your environment and you have to type it when you start it later on, so make sure it’s not too long and easy to remember.
# create a new environment conda create --name my_environment -y # create a new environment with specific python version conda create --name my_environment python=3.9 -y
You can see all environments created by you with the following command:
# list available environments conda env list
Next we can activate our newly created environment. (Of course you have to insert your environments name in the place of
# activate your environment conda activate my_environment
Install external packages with conda¶
External packages (libraries) are installed in an activated environment with the command
conda install packagename. Install the packages jupyter and jupyterlab:
# conda install packagename conda install jupyter conda install jupyterlab # you can combine multiple packages in one line, separated by a space conda install jupyter jupyterlab -y
Install external packages with pip¶
Some libraries (external packages) are not available through conda’s package manager. We can use pythons package installer pip.
First install pip within your conda environment:
conda activate name_of_your_environment conda install pip
Then we can use pip inside the activated environment:
# conda activate name_of_your environment pip install name_of_the_package
After that it should be possible to use the installed library in this environment. If it does not work, there’s a way to explicitly call pip from within the environment. See tis post on stackoverflow as a reference).
First install pip as described above. Next we will explicitly use this pip version to install the required library:
# Get the location of your environment: conda env list
This returns for example:
base /home/username/miniconda3 text * /home/username/miniconda3/envs/text
The environment with * is the active environment.
To call pip inside this environment, we have to add
/bin/pip to the path.
The command looks like this:
miniconda3/envs/text/bin/pip install packagename
Run Jupyter Lab¶
After we have installed Jupyter Lab, we can run it from within our activated environment with the command:
# run jupyter lab # (activate the environment first) jupyter lab
Depending on your needs there are some additional arguments like:
jupyter lab --browser = False
This will not open Jupyter Lab automatically in a browser. Instead it will print a URL to the terminal, which we can paste into a browser of our choice.
jupyter lab --notebook-dir = D:/
Define the directory in which you want to launch Jupyter Lab. For e.g. if you’re working on Windows and have your installation on C and your files on D.
When we’ve finished our work, we can deactivate the environment with:
# deactivate environment conda deactivate