# Python: online resources and local installation
> [name=Federico Agostini]
> [TOC]
## Online Resources
You don't need to install Python on local machine, since there are some free online resources you can use to program in Python. In addition, these solutions already come with lots of packages already preinstalled, which allows to avoid the hussle to setup your environment.
### Google Colab
The first one is [Google Colab](https://colab.research.google.com/notebooks/intro.ipynb?hl=en), which provides a working environment with lots of packages already installed. In addition, you also have free access to GPUs, which is great to work with Neural Networks. Colab is designed to be used with [Jupyer Notebooks](https://jupyter.org/), but you can also mount your Google Drive folders and use it as a full featured IDE and write your Python scripts.
In order to use Colab, you need to install the proper extension in your Google Drive. As shown in the GIF below:
1. right click in your Drive
2. select *"Other"*
3. select *"Connect other Applications"*
4. search for *"Colaboratory"*
5. install
**N.B.** *Use your personal gmail account and not the one provide by Unipd, since there are some limitations set by the admins which prevents to use some features of Colab.*

Once you have installed it, you can create a Notebook using the contextual menu in Drive, and you are able to open a Notebook (`.ipynb`) file.
### Kaggle
A similar environment is also offered by [Kaggle](https://www.kaggle.com/) (no-setup, customizable, Jupyter Notebooks environment with GPU). Kaggle also provides access to a huge number of open datasets and Machine Learning challenges.
## Local Installation
There are several ways to obtain Python and manage the different packages. Linux and Mac already have some versions and librariers of Python installed, since they are required by the operating system. However, my advice is to avoid to use them in order not to break dependencies required by the OS.
### Anaconda and Miniconda
The easiest way to get Python is to install it through [Anaconda](https://www.anaconda.com/products/distribution). This works both on Linux, Mac and Windows. It provides a graphical installer, lots of packages already installed and a graphical way ("Anaconda Navigator") to manage the different packages and environments. In addition, Jupyer Notebooks are already present.
If you want a lighter version, I suggest to use [Miniconda](https://docs.conda.io/en/latest/miniconda.html), which only contains the base packages. You can then installed all the packages you need from the terminal using `conda` or `pip` package manager.
### How to install packages and manage environments
Usually, when using Python, you create *virtual environments*. Essentially, you save all the packages you need in one place, so you can keep your developing environment isolated for the rest of you system. This solution also allows to create different environments (with different Python or libraries versions) for different projects.
There are several package managers for Phyton. The official one is `pip`; another famous one is `conda`, which is shipped through Anaconda or Miniconda distributions. There are some differences between the two. Mainly:
- `pip`
- is designed for Python packages only
- it compiles everything from source (even if now it also installs binary wheels)
- `conda`
- is able to install packages in other languages, as C, R, ...
- install binaries
You can use them both, but it is better to avoid this behaviour, since it can lead to broken environments, due to the fact that they handle dependencies in a different way.
When you install Anaconda (or Miniconda), everything is stored inside a virtual environment called `base`. If you are using Anaconda, the `base` environment already contains lots of packages preinstalled.
It is a good habit to leave the `base` environment untouched; instead, create a new one and install there all your packages. In this way, if you broke your env (due to conflicts between the packages), you can easily delete it and recreate.
```
conda create -n myenv python=3.9
conda activate myenv
```
In your new env, install everything you need using `conda` or `pip`, trying to avoid using both at the same time.
```
pip/conda install numpy pandas matplotlib scikit-learn notebook ...
```
See [this section](#My-Setup) if you want to see how I usually manage my Python environment.
### Text editors and IDEs
There is plethora of text editors and IDEs for Python:
- [Jupyer Notebooks](https://jupyter.org/) provides rich documents in which you can embed both text and code.
You can install it using your package manager:
```
pip/conda install notebook
```
and then you can lauch them by opening a terminal and calling
```
jupyter notebook
```
If you are using Anaconda, you can also start it using the graphical interface.
- [Jupyer Lab](https://jupyterlab.readthedocs.io/en/stable/) is a next version of Jupyer Notebooks. You can also install plugins to get IDEs-like functionalities.
```
pip/conda install jupyterlab
jupyter lab
```
- [VS Code](https://code.visualstudio.com/) is a free IDE from Microsoft, which can be extended by plugins to meet all of your needings (not only for Python).
- [PyCharm](https://www.jetbrains.com/pycharm/) is a paid Python IDE by JetBrains, with a free version. If you are a student, you can also get the Professional Edition for free.
- Obviously, `vim` and `emacs` are always a choice.
### My Setup
**Disclaimer:**
This is the setup I usually do when I need to install Python on a PC. This is just the results of my experience, so it is not the holy truth, neither the most clever and efficient solution.
In addition, take into account that I work on Linux and I need to use the GPU to train Neural Networks.
---
- start by installing Miniconda
- leave the `base` environemnt untouched, and create a new one
```
conda create -n myenv python=3.9
conda activate myenv
```
- in the new environment I use:
- `conda` to install everything that is not a Python package. For example, cuda libraries which are required by Tensorflow and PyTorch.
```
conda install cudnn cudatoolkit
```
- `pip` to install every Python package.
```
pip install numpy pandas matplotlib ...
```
The reason is that almost every package is available through `pip`, while it may not be present through `conda`. In addition, I always had trouble to install both Tensorflow and PyTorch in the same virtual environment through `conda`, since it always results in incompatible dependencies. With `pip` instead, I am able to solve this problem.
- as IDE I use VSCode with Python extension (plus a bunch of others)
### Notes about Windows users
I do not actually work on Windows, but if you are I suggest you to stick with Anaconda and `conda` package manager. The reason is that `pip` may fail to compile some packages in Windows, while `conda` provides binary which are already compiled.
Another good solution, is to install the [Windows Subsystem For Linux](https://learn.microsoft.com/en-us/windows/wsl/), which is an official Microsoft tool to run a GNU/Linux environment in Windows. And then, follow the instruction you would use in a Linux box.
As for know, WSL is not able to run GUI applications (the exceptions are some versions of Windows 11 and if you are in the developer program and you have preview builds) and does not have access to GPU. However, this should change in future releases.
If you opt in for this solution, VSCode has [some tools](https://code.visualstudio.com/docs/remote/wsl) to automatically integrate with the WSL.