Virtual Environment & Setup¶
1. What is a virtual Environment¶
A virtual environment is a self-contained directory that contains a specific version of Python interpreter along with the libraries and packages required for a particular project. It allows you to isolate your project's dependencies from the system-wide Python installation and other projects.
1.1. Why is it useful and when to use it¶
Virtual environments are useful when you are working on multiple projects with different dependencies or when you want to ensure that your project runs consistently across different machines. By creating a virtual environment, you can install specific versions of packages and avoid conflicts between different projects.
Gif source: giphy.com
1.2. Key benefits of using virtual environments:¶
Dependency Isolation: Each virtual environment has its own set of installed packages, which means that you can have different versions of packages for different projects without conflicts.
Reproducibility: Virtual environments allow you to create a reproducible environment for your project by specifying the exact versions of packages required. This ensures that your project will run consistently, even if the dependencies change over time.
Portability: Virtual environments can be easily shared and transferred across different machines. This makes it easier to collaborate with others or deploy your project on different systems.
Easy Cleanup: If you no longer need a virtual environment, you can simply delete the directory associated with it, without affecting other projects or the system-wide Python installation.
To create a virtual environment, you can use tools like venv (built-in with Python 3) or conda (part of Anaconda distribution). Once created, you can activate the virtual environment to start using it.
We will dive deep into miniconda installation and conda virtual environments management shortly..
2. Miniconda¶
2.1. Why Miniconda?¶
Miniconda is a lightweight version of Anaconda, which is a popular distribution of Python and its associated packages. Here are a few reasons why you might consider using Miniconda over Anaconda:
Minimal Installation: Miniconda provides a minimal installation of Python and the Conda package manager. It only includes the essential components, allowing you to have more control over the packages and dependencies you install.
Customizability: With Miniconda, you have the flexibility to choose and install only the packages you need for your specific project. This can help reduce the overall size of your environment and improve performance.
Package Management: Miniconda comes with Conda, a powerful package management system. Conda allows you to easily install, update, and manage packages, as well as create and manage isolated environments. It provides a consistent and reproducible environment for your projects.
Cross-Platform Compatibility: Miniconda is available for multiple operating systems, including Windows, macOS, and Linux. This makes it easy to work on different platforms and share your projects with others.
Community Support: Miniconda benefits from the large and active Python community. You can find a wide range of packages and resources that are compatible with Miniconda, making it easier to find solutions to your programming challenges.
While Anaconda provides a more comprehensive installation with a pre-selected set of packages, Miniconda offers a lightweight alternative that allows for greater customization and control. It is particularly useful for developers who prefer a minimalistic approach or have specific requirements for their projects.
Overall, Miniconda provides a convenient, lightweight, and efficient way to manage your Python environments and packages, making it a popular choice among developers.
2.2. Installation (Windows)¶
1. Press Windows+R to open the Run dialog box, and then type "powershell" in the text box and hit enter.
2. Copy and paste these commands in the powershell window then hit enter
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda.exe
start /wait "" miniconda.exe /S
del miniconda.exe
3. Test your installation by running `conda list`. If conda has been installed correctly, a list of installed packages appears.
Done!
2.3. Installation (Linux)¶
1. Press Ctrl+Alt+Enter to open a new terminal window.
2. Copy and paste these commands in the terminal window then hit enter
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
3. Copy and paste these commands in the terminal window then hit enter
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh
4. Test your installation by running `conda list`. If conda has been installed correctly, a list of installed packages appears.
Done!
2.4. Setup conda environment¶
Open a terminal.
Create a new Conda environment with the required packages using the following command:
conda create -n myenv pip jupyter numpy pandas matplotlib seaborn scikit-learn pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
Activate the newly created environment:
conda activate myenv
Verify that the required packages are installed:
conda list
Done!
3. Alternative: Kaggle¶
Kaggle is a popular online platform that provides a wide range of resources and tools for data science and machine learning. It serves as a hub for data scientists, machine learning practitioners, and enthusiasts to collaborate, learn, and compete in various data-driven challenges.
3.1. Why Kaggle?¶
Kaggle offers a diverse set of features and functionalities that make it a valuable platform for data science projects:
Datasets: Kaggle hosts a vast collection of publicly available datasets covering a wide range of domains. These datasets can be used for exploration, analysis, and model training.
Competitions: Kaggle hosts machine learning competitions where participants can compete to solve real-world problems and win prizes. These competitions provide an opportunity to showcase your skills, learn from others, and gain recognition in the data science community.
Kernels: Kaggle Kernels are a powerful feature that allows users to create and share executable code notebooks. Kernels provide an interactive environment where you can write code, visualize data, and document your analysis. It is a great way to learn from others, collaborate on projects, and showcase your work.
Notebooks: Kaggle provides a cloud-based Jupyter notebook environment that allows you to write and execute code directly in your browser. It eliminates the need for local setup and provides access to powerful hardware resources, including GPUs and TPUs (Tensor Processing Units), for computationally intensive tasks.
Discussion Forums: Kaggle's discussion forums provide a platform for users to ask questions, seek help, and engage in discussions related to data science and machine learning. It is a valuable resource for learning, sharing knowledge, and getting feedback from the community.
4. Better yet, Use Google Colab!¶
Google Colab is a cloud-based Jupyter notebook. It allows you to write and execute Python code directly in your web browser without the need for any setup or installation. Colab provides a free GPU and TPU (Tensor Processing Unit) for running your code, making it a popular choice for machine learning and data analysis tasks.
4.1. How to Use Google Colab¶
Open Google Colab by visiting colab.research.google.com.
Sign in to your Google account if you haven't already.
Click on "New Notebook" to create a new Colab notebook.
You can start writing your Python code in the code cells. Colab supports all the standard Python libraries and provides access to popular data science libraries like NumPy, Pandas, Matplotlib, and PyTorch.
To execute a code cell, click on the play button on the left side of the cell or press
Shift+Enter. Colab will run the code and display the output below the cell.You can add new code cells by clicking on the
+button above or below an existing cell. You can also delete or rearrange cells as needed.Colab notebooks support both code cells and markdown cells. To create a markdown cell, click on the
+button and select "Text" from the dropdown menu. You can then write your markdown content in the cell.To run a markdown cell, simply click outside the cell or press Shift+Enter. Colab will render the markdown and display it as formatted text.
Colab provides various options for saving and sharing your notebooks. You can save your work to Google Drive or GitHub, or download the notebook as a .ipynb file. You can also share your notebook with others by providing them with the notebook's URL.
If you need to install additional packages or libraries, you can use the
!pip installcommand in a code cell. Colab will install the packages and make them available for your notebook.
Google Colab provides a powerful and convenient environment for running Python code, especially for data science and machine learning projects. It eliminates the need for local setup and allows you to collaborate with others seamlessly. Give it a try and explore its features to enhance your coding experience!