19

I know that naming a Python module starting with a number is a bad idea, as stated in this other question, but I'm wondering if is it legal to do so in a Python package, not module (aka file).

For example. I want to integrate a Django website with some external APIs and I wanted to create a "third party" package, containing a file for each provider, and I don't know if calling it 3rd_party will become a headache or I should name it third_party instead, to avoid problems.

Note: I don't know if it matters, but I'm using Python 2.7

Community
  • 1
  • 1
Caumons
  • 9,341
  • 14
  • 68
  • 82

2 Answers2

30

No, it cannot. Python package and module names need to be valid identifiers:

identifier ::=  (letter|"_") (letter | digit | "_")*

Identifiers must start with a letter or an underscore.

The import statement defines the grammar for modules as:

module          ::=  (identifier ".")* identifier

Packages are a special kind of module (implemented as a directory with __init__.py file) and are not exempt from these rules.

Technically you can work around this by not using the import statement, as the importlib module and __import__ hook do not enforce the restriction. It is however not a good idea to name your package or module such that you need to use non-standard import mechanisms to make it work.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
4

Yes.

# 1/x.py dont forget 1/__init__.py
x = 42

Import it from another file

# test.py
p1 = __import__('1.x')
print p1.x.x
neuront
  • 9,312
  • 5
  • 42
  • 71