Importing a Python File by Name

January 25th, 2024
python, tech
Let's say you have a python script:
And you want to pull a section of it out into a separate file:
You would hope you could just do something like:
import "/path/to/"
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

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:

Ideological Abuse, Busyness, and the Importance of Rest

ONE WEIRD TRICK to protect yourself against cults

via Thing of Things June 18, 2024

A discussion of discussions on AI bias

There've been regular viral stories about ML/AI bias with LLMs and generative AI for the past couple years. One thing I find interesting about discussions of bias is how different the reaction is in the LLM and generative AI case when compared to "…

via Posts on June 16, 2024

Conversations I often have about parenting

Topics that come up a lot The post Conversations I often have about parenting appeared first on Otherwise.

via Otherwise June 4, 2024

more     (via openring)