1. pip and Virtual Environments¶
It is recommended to use a virtual environment to manage your project’s dependencies. This keeps your packages isolated from the global Python installation.
Creating a Virtual Environment¶
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) on your projects directory and run:
python -m venv .venvThis creates a .venv folder in your project directory.
Activating the Virtual Environment¶
Windows:
.venv\Scripts\activatemacOS/Linux:
source .venv/bin/activate
You should see your prompt change, indicating the environment is active.
Installing Packages¶
With the virtual environment activated, install packages using:
pip install package_nameReplace package_name with the name of the package you want to install (e.g., numpy).
Requirements File¶
To manage dependencies, you can create a requirements.txt file. This file lists all the packages your project depends on.
You can create it by running:
pip freeze > requirements.txtOr to create it more efficiently, take a look here,
To install all packages listed in requirements.txt, run:
pip install -r requirements.txt2. Importing Third-Party Packages¶
After installation, you can import the package in your Python script or notebook:
import package_nameFor example:
import math # pre-installed module
from datetime import datetime # pre-installed module
import random as rnd # pre-installed module
# Using imported modules
print(math.sqrt(16))
print(math.pi)
# Current datetime
now = datetime.now()
print(now)
# Random number
print(rnd.randint(1, 10))
# Common data science imports (third party)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt3. Using Jupyter Notebook¶
Jupyter Notebook is an interactive environment for running Python code.
Installation¶
For windows you can install directly from their official website, or anaconda distribution. For macOS/Linux, you can install Jupyter from their respective package managers.
Running Jupyter¶
If you are using anaconda, you can launch it from the Anaconda Navigator. For other cases, start Jupyter Notebook by running:
jupyter notebookThis will open a web interface in your browser. You can create new notebooks and run Python code interactively.
Importing Packages in Jupyter¶
You can import packages in a notebook cell just like in a script:
import pandas as pdTip: If you need to install a package directly from a Jupyter notebook in windows, use:
!pip install package_nameTip: And for macOS/Linux, you can use your package manager (like pacman or apt or brew) to install Python packages globally, but it’s better to stick with virtual environments for project-specific dependencies