π What is a virtual environment?
A virtual environment is a special directory for holding a specific version of Python and other modules.
π€ Why do we need them?
Creating a virtual environment prevents packages with different versions from interfering with each other. This prevents the dependencies from causing conflict issues that stop the app from operating smoothly.
π Types of virtual environments
venv - an inbuilt virtual environment tool from Python
pipenv - the combination of pip and venv
conda - an open-source tool for virtual environments (widely used for data science projects)
Let's explore how to create a virtual environment using conda:
π Setting up your virtual environment
1. Install conda π»
You can download and install conda from the official Anaconda or Miniconda website
2. Navigate to a directory of your choice in the terminal π
cd replace_this_with_your_directory_here
3. Create the Conda environment file π
Create a new file and name it (this one is called my_first_conda_venv.yml).
Then add the information about the environment to it:
name: my_first_conda_venv
channels:
- defaults
dependencies:
- python=3.10
- pip
- pip:
- -r requirements.txt
4. Create the requirements.txt file π
List the Python libraries you need later on into the requirements.txt file:
pandas
numpy
matplotlib
seaborn
sqlalchemy
psycopg2
jupyterlab
5. Create and activate your environment πΏ
Use these commands to set up and access your environment:
conda env create -f my_first_conda_venv.yml
conda activate my_first_conda_venv
6. Deactivate your environment π
Always shut down the environment after youβre done working in it:
conda deactivate
Happy coding!β¨