Python模块名称以数字开头 [英] Python module names starting with numerals

查看:582
本文介绍了Python模块名称以数字开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在python(.py文件)中导入模块时,它给出了语法错误.模块名称以数字开头.这是语法错误的原因吗?

When I try to import a module in python(.py file) it gives a syntax error. The module name starts with a numeral. Is that the reason for the syntax error?

推荐答案

是的,这就是语法错误的原因.无论如何,都有多种导入方法,但是最好重命名模块.

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

甚至

123foo = __import__('123foo')

它们都是语法错误.你可以做

They are both syntax errors. You can do

foo123 = __import__('123foo')

但是最好将模块重命名为foo123,然后正常地导入它.

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

这篇关于Python模块名称以数字开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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