在python中,如何导入文件名以数字开头 [英] In python, how to import filename starts with a number

查看:920
本文介绍了在python中,如何导入文件名以数字开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上有一个名为 8puzzle.py 的文件,我想将该文件导入另一个文件(在同一文件夹中,我无法将文件名更改为文件提供)。无论如何在Python中这样做?我从8puzzle import * 尝试了通常的方式,它给了我一个错误。

Basically there is a file called 8puzzle.py and I want to import the file into another file (in the same folder and I cannot change the file name as the file is provided). Is there anyway to do this in Python? I tried usual way from 8puzzle import *, it gives me an error.

错误是:

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


推荐答案

你可以做到

puzzle = __import__('8puzzle')

非常有趣的问题。我记得不要用数字命名任何东西。

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

如果你想要 import * - 你应该看看这个问题并回答

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

这篇关于在python中,如何导入文件名以数字开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆