Create and Managing Environments
Why Creating Different Environments
It is recommended to save all of your files and folders within my-private-bucket, since everything that is outside of this folder is permanently deleted after 30 days from the last access into Coding.
Create a new environment is not mandatory to run codes, i.e. you can refer to the default one. However, creating new separate environments can be beneficial when you work with several packages or libraries that can conflict each other. Another reason is that you may want to use just few libraries, so it can be convenient to create a new lighter environment in which to run your codes.
How To Create a New Environment
In order to make your new environment persistent, i.e. it does not disappear after the server shout off, you should install it within a folder, thus making it appearing to the Files and Folder manager at the left part of the screen. Everything that is saved within the Files and Folder manager is persistent throughout the sessions.
The default environment that you find in Experiment is called "base", and you can see it by opening the Terminal and typing conda env list:
In “base” some of the most used Python packages are pre-installed. You can check the list of the installed packages by typing conda list.
There are several ways to create a new environment from Terminal. Here you will see two of the most popular.
Create and manage an environment with Python
To create a new environment called “my_env” with Python you can run the following command on terminal:
python -m venv /home/jovyan/my_env
At this point, a new folder containing the Python environment is created:
Before to use your new environment, you need to activate it.
By activating your environment, you are pointing to it when installing a new package or library.
To activate the new environment, type the command:
source /home/jovyan/my_env/bin/activate
You can see that the new environment is successfully activated from the environment name (my_env) appearing in brackets:
Now you can install packages with the pip command, as showed in the following:
pip install package_name
Create and manage an environment with conda
Another way to create an environment is by using the “conda” command:
conda create -p ./my_env
This will create a new environment called “my_env” in the current working directory. It is also possible to create the new environment with some libraries already installed. For example, you can create the new environment with Python installed on it, by running the following command:
conda create -p ./my_env python
To activate the new environment, you can type the following command:
source activate /home/jovyan/my_env
Since the command conda create -p ./my_env creates the environment within the current working directory, ensure what is your current directory typing pwd.
You can also check the new environment installation path with the command conda env list.
To install packages on the new environment, you can type the following command after having activated it:
conda install package_name
You can also specify the channel and the package version to be installed. For more information about this last, please refer to the related documentation.
Install from .yml file
Another very useful option is to install a new environment from an existing one (in yml format). To do so, you can use the following command:
conda env create -p installation_path -f path_of_the_file_name.yml
Where installation_path refers to the desired place in which the new environment must be installed, and path_of_the_file_name.yml refers to the path in which the .yml file has been uploaded on the Jupyter Notebook.
Delete an environment
To delete an existing environment, you can run the command:
conda env remove -p environment_path
Where environment_path refers to the installation path of the environment to be deleted.
Export an existing environment
You can export your environments in .yml files. To do so, first activate the chosen environment from Terminal, then run the command:
conda env export > environment.yml
In this case the current environment will be exported in a .yml file called “environment” and saved to the current working directory.
In case you are interested in exporting environments without packages build information, the command to run is the following:
conda env export --no-builds > environment.yml
Setting Up an Environment as a Kernel
To configure an existing environment as a kernel, follow the appropriate procedure based on your chosen programming language. The examples below cover Python and R.
-
Python:
-
In the terminal, activate the environment you want to use as a kernel.
-
Run the command
conda install ipykernel -
Now run
python -m ipykernel install --user --name my_env --display-name "Python (my_env_kernel)"
-
R:
-
In the terminal, run
Rto enable R commands -
Run the command
install.packages(“IRkernel”) -
Now run
IRkernel::installspec(name = "my_env", displayname = "R (my_env_kernel)")
Once this is done, you will see the new kernel on the Launcher page.
Now you can select the new kernel to be used in a notebook:
Delete Kernels
To see the available kernels for the selected environment, you can run the following command from terminal:
jupyter kernelspec list
Now to delete the “my_env” kernel, just run the command:
jupyter kernelspec uninstall my_env
Additional Resources
-
JupyterLab Documentation: For a deeper dive into using JupyterLab functionalities, refer to the official documentation: https://readthedocs.org/projects/jupyterlab/
- MAAP Github: Here you find the offical MAAP Github repository: https://github.com/MAAP-Project






No comments to display
No comments to display