在 python 中,如何导入文件名以数字开头

基本上有一个名为 8puzzle.py的文件,我想导入文件到另一个文件(在同一个文件夹中,我不能改变文件名作为文件提供)。有没有什么方法可以在 Python 中实现这一点呢?我试了 from 8puzzle import *常用的方法,它给我一个错误。

错误是:

>>> import 8puzzle
File "<input>", line 1
import 8puzzle
^
SyntaxError: invalid syntax
>>>
57856 次浏览

Don't use the .py extension in your imports.

Does from 8puzzle import * work?

For what it's worth, from x import * is not a preferred Python pattern, as it bleeds that module's namespace into your current context.

In general, try to import things you specifically want from that module. Any global from the other module can be imported.

e.g., if you have 8puzzle.foo you could do `from 8puzzle import

Edit:

While my .py message is correct, it isn't sufficient.

The other poster's __import__('8puzzle') suggestion is correct. However, I highly recommend avoiding this pattern.

For one, it's reserved an internal, private Python method. You are basically breaking the fundamental assumptions of what it means to be able to import a module. Simply renaming the file to something else, like puzzle8, will remedy this.

This will frustrate the hell out of experienced Python programmers who are expecting to know what your imports are at the top and are expecting code to (try to) conform to PEP8.

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.

The above answers are correct, but as for now, the recommended way is to use import_module function:

importlib.import_module(name, package=None)
Import a module. The name argument specifies what module to import in absolute or relative terms (e.g. either pkg.mod or ..mod). If the name is specified in relative terms, then the package argument must be set to the name of the package which is to act as the anchor for resolving the package name (e.g. import_module('..mod', 'pkg.subpkg') will import pkg.mod).

The import_module() function acts as a simplifying wrapper around importlib.__import__(). This means all semantics of the function are derived from importlib.__import__(). The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.mod), while __import__() returns the top-level package or module (e.g. pkg).

If you are dynamically importing a module that was created since the interpreter began execution (e.g., created a Python source file), you may need to call invalidate_caches() in order for the new module to be noticed by the import system.

__import__ is not recommended now.

importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0)
An implementation of the built-in __import__() function.

Note Programmatic importing of modules should use import_module() instead of this function.

The file directory structure is as follows:

 daily
-- 20210504
permutations.py
__init__.py
__init__.py

You can import the permutations module by __import__ or importlib.import_module.

The official documentation recommends using importlib.import_module.

import(name, globals=None, locals=None, fromlist=(), level=0) -> module

Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to useimportlib.import_module()to programmatically import a module.

What is the difference?

If implemented using __import__. For example:

res = __import__('daily.20210504.permutations')

The result of res is the daily module. daily module

So, if you want to get the permutations module, you need to provide the fromlist parameter, which is written as follows.

res = __import__('daily.20210504.permutations', fromlist=('daily.20210504'))

The result of res can be seen now as enter image description here That's the right result.

What if I use importlib.import_module?

res = importlib.import_module('daily.20210504.permutations')

this allows you to get the permutations module directly.