Created on 05 Mar 2013 ;    Modified on 05 Mar 2013 ;    Translationitalian

python: do not use dashes (-) in file names

The problem

When programming in Python, you have to avoid using hyphens in names of files that can be imported (modules).

For what reason?

Let's see.

Suppose you have a file named, to import in the file

In we write:

 import my-modulo

But python interprets te - sign as the subtraction operator. As a result we obtain the signal syntax error, since the import statement doesn't expect an expression.

So avoid the dashes in the names of the modules. If you need to use them, to import you must use the statement

  mymodule = __ import__ ("my-module")

That I would not recommend. It makes less readable the code, and it is an infrequent statement, therefore less known to the general public of Python.


How to import python module when module name has a '-' dash or hyphen in it?