Jiminion Jiminion - 1 month ago 11
Python Question

Using from in Python (directory name begins with a number, invalid identifier)

I am just trying to import from different directories.
This works:

if (ssize == 32):
from Python_32bit import mycoolmodule
else:
from Python_64bit import mycoolmodule


But this does not:

if (ssize == 32):
from 32bit_Python import mycoolmodule
else:
from 64bit_Python import mycoolmodule

Answer

Valid module name have to be correct identifier.

Identifiers follow lexical notation defined as:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"
  • Python_32bit is a valid identifier.
  • 32bit_Python is not (it starts with a number, while identifiers have to start with (letter|"_")).
Comments