Python模块名称以数字开头

编程入门 行业动态 更新时间:2024-10-25 10:33:34
本文介绍了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模块名称以数字开头

本文发布于:2023-11-06 05:24:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:开头   模块   名称   数字   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!