What are the requirements for naming python modules?

后端 未结 3 861
醉梦人生
醉梦人生 2021-01-14 10:25

I\'ve been going through Learn Python The Hard Way as a sort of refresher. Instead of naming each example ex#.py (where # is the number of the exercise), however, I\'ve just

3条回答
  •  春和景丽
    2021-01-14 11:04

    Strictly speaking, you can name a Python file anything you want. However, in order to import it using the import statement, the filename needs to be a valid Python identifier --- something you could use as a variable name. That means it must use only alphanumerics and underscores, and not start with a digit. This is because the grammar of the import statement requires the module name to be an identifier.

    This is why you didn't see the problem until you got to an exercise that requires importing. You can run a Python script with a numeric name from the command line with python 123.py, but you won't be able to import that module.

提交回复
热议问题