Python module names starting with numerals [duplicate]

徘徊边缘 提交于 2019-12-13 05:04:59

问题


When I try to import a module in python(.py file) it gives a syntax error. The module name starts with a numeral. Is that the reason for the syntax error?


回答1:


Yes, that is the reason for the syntax error. There are various ways of importing it anyway, but it's better to rename the module.

The reason is that variable names can't start with a numeral. Hence you can't do

import 123foo

or even

123foo = __import__('123foo')

They are both syntax errors. You can do

foo123 = __import__('123foo')

But it's better to just rename the module to foo123 and import it normally instead.




回答2:


Yes. To avoid this, you can do __import__("number"). For example:

mymodule = __import__("1234")

Which would be the same as:

import 1234 as mymodule

Without the SyntaxError, of course.

You can read more about it here.



来源:https://stackoverflow.com/questions/16644226/python-module-names-starting-with-numerals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!