70

From Building Skills in Python:

A file name like exercise_1.py is better than the name exercise-1.py. We can run both programs equally well from the command line, but the name with the hyphen limits our ability to write larger and more sophisticated programs.

Why is this?

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
atp
  • 30,132
  • 47
  • 125
  • 187
  • 4
    Incidently, the author of that particular document is one of the top-ranking Python answerers on this site: http://stackoverflow.com/users/10661/s-lott – ire_and_curses Apr 29 '10 at 20:38
  • Related discussion on SuperUser: [Using underscore in file names vs dashes?](https://superuser.com/q/266491/654416). – codeforester Apr 12 '18 at 20:57
  • This is a good read as well, on Unix & Linux: [Good style/practices for separators in file (or directory) names](https://unix.stackexchange.com/q/44153/201820) – codeforester Apr 12 '18 at 21:03

2 Answers2

82

The issue here is that importing files with the hyphen-minus (the default keyboard key -; U+002D) in their name doesn't work since it represents minus signs in Python. So, if you had your own module you wanted to import, it shouldn't have a hyphen in its name:

>>> import test-1
  File "<stdin>", line 1
    import test-1
               ^
SyntaxError: invalid syntax
>>> import test_1
>>>

Larger programs tend to be logically separated into many different modules, hence the quote

the name with the hyphen limits our ability to write larger and more sophisticated programs.

homocomputeris
  • 509
  • 5
  • 18
Daniel G
  • 67,224
  • 7
  • 42
  • 42
24

From that very document (p.368, Section 30.2 'Module Definition'):

Note that a module name must be a valid Python name... A module's name is limited to letters, digits and "_"s.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141