Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do hyphens in module names generate syntax error?

Tags:

python

syntax

I'm using python 2.6 and get the following when importing a module:

  File "./test-nmea-uploader.py", line 11
    import nmea-uploader as sut
               ^
SyntaxError: invalid syntax

Why is that so? The python style guide seems to hold no mention about using hyphens in names, although it suggest the use of underscores.

Alan

like image 998
a1an Avatar asked Jan 30 '12 13:01

a1an


1 Answers

According to http://docs.python.org/reference/lexical_analysis.html#identifiers, any identifier must start with a lowercase/uppercase letter or an underscore and contain lowercase/uppercase letters, numbers or an underscore.

Package names are identifiers, so they are bound to the same rules.

In addition, nmea-uploader might also mean the subtraction of uploader from nmea. Since you can import a package anywhere in a python file, if you have defined the variables nmea and uploader in advance, the interpreter would get confused if a hyphen was allowed for identifier names.

like image 114
Alexandros Avatar answered Oct 16 '22 01:10

Alexandros