Importing a Python File by Name

January 25th, 2024
python, tech
Let's say you have a python script:
2024-01-24--evaluate-chimeras.py
And you want to pull a section of it out into a separate file:
2024-01-25--strand-split-artifacts.py
You would hope you could just do something like:
import "/path/to/2024-01-25--strand-split-artifacts.py"
But this doesn't work: import wants a module name, not a filename. The simplest way I know to import a python file from a path is:
import sys
import importlib

sys.path.append("/path/to/")
ssa = importlib.import_module("2024-01-25--strand-split-artifacts")
There are a lot of complicated ways to do this, some of which avoid needing to add something to sys.path, but for quick one-off research code better to keep it simple.

Comment via: facebook, lesswrong, mastodon

Recent posts on blogs I like:

Somewhat Against Trans-Inclusive Language About Biological Sex

"People with vaginas"? Well, maybe

via Thing of Things April 25, 2024

Clarendon Postmortem

I posted a postmortem of a community I worked to help build, Clarendon, in Cambridge MA, over at Supernuclear.

via Home March 19, 2024

How web bloat impacts users with slow devices

In 2017, we looked at how web bloat affects users with slow connections. Even in the U.S., many users didn't have broadband speeds, making much of the web difficult to use. It's still the case that many users don't have broadband speeds, both …

via Posts on March 16, 2024

more     (via openring)