简体   繁体   English

如果文件名或文件路径包含非alpha,非下划线字符,如何在python中导入?

[英]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? 在这种情况下如何进行输入?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM