Anaconda and Jupyter Notebook
I want to share a powerful tool, as you can see from the title of this post Anaconda, this is an open-source that has many Python applications in it. If you are learning or using Python, this is a great tool to debug your code. Many people should have used it, but I still want to write this post on many commands that are able to use.
Basely Anaconda is a tool used for Python use in the Data Science platforms that many people will be using especially with Jupyter Notebook or Jupyter Lab. It’s pretty useful if you’re a beginner learning Python.
Inside Anaconda has different applications such as Jupyter Notebook, Spyder and more. It contain hundred of python package
Anaconda
Download and Installation
Let's Download Anaconda on the official website https://www.anaconda.com/products/distribution
Let open the anaconda Application after installing it, which looks like this:
After you have installed Anaconda you can open it which you see above, this is called Anaconda navigator. You can use this to install the python package or install other Applications. But I think before navigator is quite useful which is like GUI interface, but right now it become pretty slow.
There are two ways you can use Anaconda, which I mention above the navigator, another one is below using cli command. This tool supports Mac, Windows, and Linux platforms.
I will show the most commonly used setting, but I personally prefer using the CLI command, which is faster and memory will not burst up, this is my opinion.
Please Note Anaconda Navigator is just like a GUi, but we are also able to use the CLI command using conda
command. I will introduce in below. This conda
command is just like application management just like Python’s pip command.
Anaconda Navigator
Below the left side is an application, and the right side is the virtual environment. As you can see the application can see all applications which will show all application Anaconda support, if installed it will display launch, not installed will display install.
You can select an application to remove or update a specific version
- Virtual Environment Setting
The left side is Virtual Environment, and the right is the Python package
Let create a virtual Environment
- install and remove the Python package
We can install a specific package or remove it.
- Remove package:
- Install new package
- anaconda configure
It is about the basic of Anaconda, below I am going to show you using the command prompt mentioned or CLI command to run or execute it.
Anaconda CLI command
I want to share most often used the CLI command I used often, which allows installing Python package or open andaconda’s application such as Jupyter Notebook.
We need to set the environment variable in order to use conda
this command in CLI. C:\Users\<username>\anaconda3\Scripts
If you can’t find how to set the environment variable press window+R and enter sysdm.cpl
will open system properties, please go to adance and click environment variable
After adding it, open the terminal, then type in conda
look like this:
Conda commands
- conda version:
conda --version
- show config:
conda config --show
- check conda setting:
conda info
- list all packages:
conda list
- search package:
conda search <package>
- Install package
conda install <packages >
conda install conda = 4.6.11
conda install --yes --file requirements.txt
- Conda Update:
conda update --all --yes
conda update -n base -c defaults conda
update specfic application:
conda update anaconda
- remove package
conda remove <package>
conda remove -n <env_name> <package>
- install Mamba Package ( Fast Cross-Platform Package Manager)
conda install -c conda-forge mamba
mamba install -c conda-forge <pkg>
- Export package
$ conda list -e > requirements.txt
$ conda env create -n <env_name> -f requirements.txt
$ conda env export > freeze.yml
- Virtual environment command
Why do we need virtual env? If we have many python packages installed some version packages might conflict with each other, so we need to create a virtual environment. Each environment will have an individual package, you can refer it as a new install python with no other package installed.
Create Virtual environment basic way
conda create -n <env_name>
-n
or–name
: virtual environment name
Add a package or assign a specific python version
conda create -n <env_name> <package>
conda create -name <env_name> python=<version>
or
conda create — name XXX python — no-default-packages
example:
conda create --name test python=3.5 scipy=XXXX
we can also use this conda env create -f environment.yml
Remove Virtual environment
Use with the command is ok
conda remove — name myenv — all
conda remove -n myenv — all
List and activate virtual Env:
- list all conda env (use either command):
conda env list
conda info --envs
conda env list
- activate env:
conda activate|deactivate myenv
Activate Default virtualenv:
activate
you will see(base)
which is root or defaultActivate virtual environment:
activate <myenv>
Exit or deactivate virtual environment :
deactivate
- Switch virtual env
Window:
conda activate <env name>
Linux/mac:
source activate <env name>
- Export/import virtual environment
Export:
conda env export –n <env_name> -f environment. yml
import:
conda env create -f environment.yml
Virtual Environment problem and solution
[update]
Last week I faced a problem on Anaconda which is when I try to list my virtual environment the name or prefix is empty only showing the path. Today i am going to teach how to solve this solution to name the virtual env.
some commands i had mentioned before, i will still mention again to be more convenient, so that you don’t have to scroll up.
show virtualenv:
conda env list
Let look at below picture, you will have notice that the virtual env name is empty except the base which is the root. If you launch Ananconda Navigator or GUI will see the name exist.
By the way since there is no name, it’s stil possible to access it by using the -p
option which is the path.
The solution to this I will mention two methods.
- Method 1: clone the existing path and rename it.
This method will occur duplicate, so you just have to delete the old one
command: conda create --clone <exist env path> -n <new vir env name>
let show our list again, will see the new old obtain name,but old one still no name.
in case you want to delete the old virtual env or the path use -p option below( i will provide remove name and path, you can decide using which one>:
remove Path -p:
conda env remove -p </pathname>
remove name:
conda env remove -n <envname>
- Method2: name the virtual env
You can use the rename methods and use the -p option to name a new virtual name: conda rename -p <virtual env path> <rename virual name>
this method will not have a duplicate environment, which you don't have to remove it. Basely it’s just rename the empty name to a new name
Reference please refer below for more detail
https://stackoverflow.com/questions/49127834/removing-conda-environment
Jupyter NoteBook
After knowing how to use the conda CLI command, we can use the conda CLI command to open or install it. But before showing the CLI command, I want to show you without using the CLI command. There are two methods:
- Open Anaconda and run Juptyer Notebook
- Run Jupyter Notebook
You might be curious since we can run jupyter notebook, why do we need to open anaconda instead? Well this is a pretty good question, but somehow it depends on people, but opening conda you can install or update the package. I think the best way is to use the CLI command.
Using Anaconda to install packages sometimes is pretty slow, so the best solution is to use the command instead.
you can download or install Jupyter Notebook https://jupyter.org/ or install using anaconda
JupyterNotebook configuration
- generate notebook setting or configure, if not will use the default setting
generate config file:
jupyter notebook — generate-config
,Generate location:
C:\Users\<username>/.jupyter\jupyter_notebook_config.py
- disable password :
c.NotebookApp.password = ‘’
default is mark # , you can change it.
- change default browser(useful)
I use chrome as an example, you can change what you prefer.
c.NotebookApp.browser = ‘C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s’
- change default directory
Change specific directory when open jupyter notebook:
c.NotebookApp.notebook_dir=’C:\\new\\jupyternotebook’
since I editor above directory location, all my jupyter Notebook files will be stored in this place. When you open Jupyter Notebook, you can use pwd
to check as below.
If you want to know more about the configuration, please refer the documentation for more clear explanation:https://jupyter-notebook.readthedocs.io/en/4.x/public_server.html
Run Jupyter notebook
- Run Jupyter-notebook
activate conda:
conda activate
jupyter-notebook
or open anaconda navigator and click Lanuch on Jupyter Notebook
you can also run on Windows and search Jupyter Notebook.
After launching it, it will look like the below-left picture. In order to create a python code or text click the new, in my case I click python. If you want to write and execute python code please refer to right bottom picture.
it also supports markdown language. You Just change the Code into Markdown, type markdown then click the run will generate the markdown format, please refer below picture.
Jupyter notebook command
- Run jupyter Lab:
jupyter-lab
- check your jupyter path:
jupyter --paths
- check jupyter configuration :
jupyter — config-dir
- list URL token:
jupyter notebook list
you can check python package’s version just just like pip -- list
but inorder to use it under notebook, you just have to type it like this:
!pip list -o
Reference
https://jupyter-notebook.readthedocs.io/en/4.x/public_server.html
https://stackoverflow.com/questions/41159797/how-to-disable-password-request-for-a-jupyter-notebook-session
https://stackoverflow.com/questions/35860436/create-empty-conda-environment
Conclusion
This is a pretty basic setting and command of Anaconda I use often, especially many commands might forget. So I decided to write a post about most of the important commands or settings. There are many more settings I didn’t mention here, but I think most of the things I cover should be enough that can cover 70 percent of writing Python.
If you don't want to use IDE or text editor, instead, you can use Anaconda for it. Sometimes when I am testing code, I will use Jupyter Notebook instead of Visual Studio or text editor, because it’s more convenient. Hope this post might help people understand the fundamental setting.
<Update>
Honestly, these couple of months I literally changed from Jupyter Notebook to Jupyter Lab it was fantastic. I much like using Jupyter Lab because it allows me to drag file, especially csv on doping pandas work pretty awesome and convenient, just wants to share it with people.