问题
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