Using your environment¶
Installing sndfile
from PyPi¶
Let's try to install sndfile
from PyPi again, this time in the activated Spack
environment. The advantage we have here specifically with Python is that we
don’t need an additional
Python virtualenv.
Any pip install
commands will install Python libraries inside the Spack
environment:
pip install
output
[spack-python] $ pip install sndfile
Collecting sndfile
Using cached sndfile-0.2.0.tar.gz (4.3 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Installing backend dependencies ... done
Preparing metadata (pyproject.toml) ... done
Collecting cffi>=1.0.0 (from sndfile)
Using cached cffi-1.17.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
Collecting pycparser (from cffi>=1.0.0->sndfile)
Using cached pycparser-2.22-py3-none-any.whl (117 kB)
Building wheels for collected packages: sndfile
Building wheel for sndfile (pyproject.toml) ... done
Created wheel for sndfile: filename=sndfile-0.2.0-cp312-cp312-linux_x86_64.whl size=29722 sha256=95ee841571f4befe79b8ac2aca07787b147c82fcd433bcd981d9af88ab567fa3
Stored in directory: /data/scratch/abc123/.cache/pip/wheels/3c/ce/44/b9c297d8a032a4f7ff6eefb880458d397d66e14b94d600d3bd
Successfully built sndfile
Installing collected packages: pycparser, cffi, sndfile
Successfully installed cffi-1.17.1 pycparser-2.22 sndfile-0.2.0
[spack-python] $ ls -1 /data/scratch/abc123/spack-environments/spack-python/.spack-env/view/lib/python3.12/site-packages
cffi
cffi-1.17.1.dist-info
_cffi_backend.cpython-312-x86_64-linux-gnu.so
pip
pip-23.1.2.dist-info
pycparser
pycparser-2.22.dist-info
README.txt
sndfile
sndfile-0.2.0.dist-info
Success! And indeed, we are able to import the sndfile
library in an
interactive Python shell inside the activated Spack environment:
Using sndfile
in a Python shell
[spack-python] $ python
Python 3.12.1 (main, Jun 6 2024, 15:45:45) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sndfile
>>> import pathlib
>>> SAMPLES = pathlib.Path("/data/scratch/abc123/audio")
>>> with sndfile.open(SAMPLES / "sample.wav", "r") as sample:
... print("frames: {}".format(sample.frames))
... print("format: major={0[0]}, minor={0[1]}".format(sample.format))
... print("samplerate: {}".format(sample.samplerate))
... print("channels: {}".format(sample.channels))
... print("sections: {}".format(sample.sections))
...
frames: 698194
format: major=wav, minor=default
samplerate: 44100
channels: 2
sections: 1
>>>
In a job script or interactive job¶
Now that your environment is created, you can activate it and use it in a job script or interactive job with the following lines:
module load spack/<version>
spack env activate /data/scratch/abc123/spack-environments/spack-python
python script.py
The -p
flag is not required in job scripts because the decorated prompt will
not be observed within non-interactive jobs. The sndfile
Python library should
be available for any Python scripts you run as long as you activate your Spack
environment as part of the job script or during the interactive job.