简体   繁体   中英

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

Am I allowed to have a directory 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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