开发者

Can Python directory names be keywords? E.g. 'import'?

开发者 https://www.devze.com 2022-12-27 06:25 出处:网络
Am I allowed to have a director开发者_Python百科y named \'import\' containing Python code?Or will the import command fail to parse it as a result?Is there any way around that?You can use the built-in

Am I allowed to have a director开发者_Python百科y named 'import' containing Python code? Or will the import command fail to parse it as a result? Is there any way around that?


You can use the built-in __import__ function which accepts any string. Thus you may write:

__import__('keyword.submodule')


You can have a directory with a name that is a Python keyword storing your Python code. This directory should not be used as a package, since package names should be valid Python identifiers.


Or will the import command fail to parse it as a result?

It will indeed fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消