2

Is it possible to import a module that contains '-' characters in the filename?

i.e.

import my-python-module

or do you have to rename the file, i.e.

mv my-python-module.py my_python_module.py

and then..

import my_python_module
bph
  • 10,728
  • 15
  • 60
  • 135
  • 2
    You can't and really should not bother dealing with something like that. The module should conform to Python's rules. – idjaw Oct 17 '16 at 13:05
  • I assume not spotting a duplicate question is a down-votable offence? As opposed to the question being poor (the other identical question has a score of 52). I guess duplicate questions do waste some peoples time so probably fair enough? – bph Oct 17 '16 at 13:18
  • turns out it isn't - http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – bph Oct 17 '16 at 13:40

1 Answers1

5

From PEP 8 -- Style Guide for Python Code:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

PEP 8 is the more-or-less "official" coding standard for Python code.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • ahh - i see. I think the developer came from a lisp/emacs background so using '-' in the filenames would have seemed quite normal.. won't be a problem to rename – bph Oct 17 '16 at 13:09
  • 1
    It might be worth mentioning (especially to new python programmers) what PEP 8 is: the 'official' code standard guidelines. If everyone sort-of-keeps to them, then code will be a lot more reusable and easily read/written/debugged by other Python programmers. (I was just looking up the link when you answered:P) – Jmons Oct 17 '16 at 13:09