r/learnpython • u/DaennoBlatt • 1d ago
Terminal Access?
Hi, I am using Python with Jupyter Notebooks on MacBook. Is it possible to perform tasks you would normally do in the terminal within python. In my explicit case, I want to use SExtractor (Source extractor that finds light sources in image...) and then perform calculations with the created source table. But SExtractor does not have a visual interface and just works via inputs in the terminal. Is it possible to perform those taks within a python script if you know what I mean?
4
u/Franckyboi85 23h ago
Yeah, in a Jupyter cell you can put a ! in front of the command and it runs in the shell, handy for testing. For a proper script subprocess.run like the other comment said is the way, then astropy can read the SExtractor catalogue straight into a table
3
1
u/Sufficient-Tale6131 17h ago
The subprocess route the others mentioned is the way to go. A few things that tend to trip people up with SExtractor specifically:
Depending on the version and how it was installed, the executable is called source-extractor (newer releases) or sex (older ones). shutil.which("source-extractor") returns None if Python can't find it.
Jupyter started from Anaconda Navigator or VS Code on a Mac often doesn't get the same PATH as your Terminal, so a command that works in Terminal fails with FileNotFoundError in the notebook. Passing the full path (run which source-extractor in Terminal to get it) avoids that.
Something like subprocess.run(["source-extractor", "image.fits", "-c", "default.sex"], cwd="path/to/workdir", check=True) keeps the output in a known folder and raises an error if the run fails instead of silently doing nothing. SExtractor also looks for default.param and default.conv in the working directory, which is why cwd matters.
After that, astropy reads the catalog with the column names from the header: Table.read("test.cat", format="ascii.sextractor") (that works with the default ASCII header catalog type).
If you'd rather stay entirely in Python, the sep package is a library version of the core SExtractor algorithms (background estimation, source extraction, aperture photometry) that works directly on numpy arrays, so there's no config file or external program at all.
10
u/deanominecraft 1d ago
the subprocess module (part of standard library) is probably what you are looking for