Elliot
Elliot

Reputation: 5561

How does one import in python if the filename or filepath contains a non-alpha, non-underscore character?

I need to import a function from a file which is in a folder whose name contains a special character, with the file itself also containing a special character. I cannot change the names to correct the issue. How does one perform the importation in such a situation?

Upvotes: 7

Views: 8705

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208545

Yeah that is some horrible naming, but this should work for you:

Instrument_Control = __import__("Instrument-Control.audio$pecial")
audiospecial = getattr(Instrument_Control, "audio$pecial")
print audiospecial
# <module 'Instrument-Control.audio$pecial' from 'Instrument-Control/audio$pecial/__init__.py'>
audiospecial.example_func()  # executes example_func() from audio$pecial

Upvotes: 7

Related Questions