KelvinS
KelvinS

Reputation: 3071

Convert module path to import_module name format

Is there a built-in or simple way to convert a file path (module path) to a name argument to be used by the importlib.import_module function?

For example:

'path/module.py' -> 'path.module'

I could do something like this:

if path.endswith('.py'):
    path = path[:-3]

path = path.replace('/', '.')

But I was looking for a simpler way.

Upvotes: 3

Views: 644

Answers (1)

BpY
BpY

Reputation: 433

Would:

path.split('.py')[0].replace("/", ".")

be suitable?

Upvotes: 1

Related Questions