In Python, How to Import Filename Starts with a Number

In python, how to import filename starts with a number

You could do

puzzle = __import__('8puzzle')

Very interesting problem. I'll remember not to name anything with a number.

If you'd like to import * -- you should check out this question and answer.

Python module names starting with numerals

Yes, that is the reason for the syntax error. There are various ways of importing it anyway, but it's better to rename the module.

The reason is that variable names can't start with a numeral. Hence you can't do

import 123foo

or even

123foo = __import__('123foo')

They are both syntax errors. You can do

foo123 = __import__('123foo')

But it's better to just rename the module to foo123 and import it normally instead.

How to import a Python file with a variable name?

Use the standard library package importlib:

import importlib
import sys
vt = importlib.import_module(sys.argv[2])

Can a Python package name start with a number?

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.

Is it acceptable to have python package names with numbers in it?

First, note that PyPI project names and module names are completely independent; there's nothing stopping you from creating a package foo that installs a module bar, and these two names follow separate policies as to what is valid.

Module names are restricted by Python's grammar to be valid identifiers. In Python 2, this means that they must consist of an ASCII letter or underscore followed by zero or more ASCII letters, digits, and/or underscores. In Python 3, Unicode is added, and things get more complicated, but I believe that all-ASCII module names still follow the same restrictions.

The names of projects on PyPI (as specified in PEP 508, among others) must consist entirely of ASCII letters, numbers, ., -, and/or _, and they must begin & end with a letter or number. There is also a normalization policy that enforces case-insensitivity and treats runs of ., -, and _ as equal, so foo-bar and FOO.BAR are considered the same project.

In addition, PEP 8 has a section on package and module names; it says:

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.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).

So, yes, you can have a number in both a project name and a module name, and the project name can even begin with one!

Python Import function from file

Best answer: rename your file. ;)

Having a file named 8.py breaks the naming convention for files as well as Python's language grammar (your trouble importing it makes it clear why these rules are in place).

Generally, you should make filenames the same as variables:

  1. They start with a letter or underscore.
  2. After that, they are composed entirely of letters, numbers, and underscores.
  3. They are lowercase.
  4. They are short (nobody likes a huge name to import).

However, if you must name the file 8.py, you can use __import__ to import it.

To demonstrate, I made a simple 8.py file that had the following function:

def func():
return True

Here is the test I ran:

>>> from 8 import func
File "<stdin>", line 1
from 8 import func
^
SyntaxError: invalid syntax
>>> x = __import__("8")
>>> x.func()
True
>>>

As you can see, using __import__ works. However, it is considered sloppy and should generally be avoided. I strongly recommended that you heed my first answer and rename the file if you can.



Related Topics



Leave a reply



Submit