zrobot
zrobot

Reputation: 45

Can module names in Python end with an underscore?

Below you can find an example of the problem that I'm facing:


project:


xml.py:

import xml.etree.ElementTree as Et


My module named xml.py is getting confused with the xml package when I import it, now my question is:

Can I name my module in such a way that won't interfere with the package name, but at the same time would be compliant with the PEP8 styling guide (e.g: xml_.py. I have seen this naming convention for variables but I am not sure it is a good practice to name modules like this).

Upvotes: 1

Views: 1558

Answers (2)

mabergerx
mabergerx

Reputation: 1213

The PEP-8 guide on module names states:

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.

Moreover, another PEP-8 guideline states:

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.:

... and that's exactly why you are doing it, and not just "for fun". So, even though it is not explicitly stated as a naming guidelines for modules, I assume we can adapt it to all Python naming conventions. So I think you are good to go

Upvotes: 1

Mac78
Mac78

Reputation: 13

modules (filenames) should have short, all-lowercase names, and they can contain underscores;

Pep8 documentation, so yes you can :)

Upvotes: 0

Related Questions