A Python virtual environment is a personal isolated Python installation. The virtual environment consists of a directory that contains all the necessary executables for a Python project. You can customize this environment by installing additional Python packages.
These instructions are for creating the Python virtual environment, typically, done once. Once created, activate from within a slurm batch script or inside an interactive slurm job.
See folder 12-python-duckdb from the examples for reference
Always create virtual environments in your ~/bigdata directory. If you do not have a ~/bigdata directory, email scrc-list@stern.nyu.edu to request one.
Load the Python environment from which to create the virtual environment. Create a directory for the virtual environment. Create the virtual environment.
module load python/3.9
mkdir ~/bigdata/05-virtenvs
python -m venv ~/bigdata/05-virtenvs/py3.9
To activate:
source ~/bigdata/05-virtenvs/py3.9/bin/activate
(py3.9)[user123@login-node-1 ~] $
Notice the name of the active virtual environment is prepended to your prompt
pip list
pip install duckdb
python <my-prog.py>
deactivate
Run your Python code in a batch or interactive Slurm job on the SCRC slurm cluster or from your research project virtual machine.
Do not run analyses directly on a login node.
#!/usr/bin/env python
#csv-join.py
import duckdb
# --------------------------------
# main
# --------------------------------
def main():
# Join CSV files and print results
result = duckdb.sql("""
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.product,
o.amount
FROM 'customers.csv' c
INNER JOIN 'orders.csv' o
ON c.customer_id = o.customer_id
""")
# Display results in table format
print("Joined Customer Orders:")
result.show()
if __name__ == '__main__':
main()
SLURM Script
#!/bin/bash
#
# [ csv-join.sbatch ]
# This script runs a Python program that uses DuckDB to join two CSV files,
# customers.csv and orders.csv, and prints the joined table.
#
# It needs a Python virtual environment with duckdb installed. See the
# Python Virtual Environment tutorial to create one.
#
# Submit it with:
# sbatch csv-join.sbatch
#SBATCH --job-name=csv-join
#SBATCH --mem=1g
#SBATCH --time=00:10:00
#SBATCH --output=csv-join-%j.out # output file; %j = job ID
#SBATCH --mail-type=END,FAIL
#SBATCH --mail-user=netID@stern.nyu.edu
module purge
source ~/bigdata/05-virtenvs/py3.9/bin/activate # change to the path of your virtual environment
python csv-join.py
Submit the job
$ sbatch csv-join.sbatch