Python不接受用“”导入模块名

2024-05-29 02:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“downloadflat”的模块-模块化.py,python无法导入此模块

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import downloadflat-modular
  File "<stdin>", line 1
    import downloadflat-modular
                       ^
SyntaxError: invalid syntax
>>> from downloadflat-modular import *
  File "<stdin>", line 1
    from downloadflat-modular import *
                     ^
SyntaxError: invalid syntax
>>> 

这是限制吗?你知道吗

uname -a
Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Tags: 模块frompyimportlinuxstdinline模块化
3条回答

包名是标识符,因此不能包含连字符。你知道吗

python中的标识符必须以小写/大写字母或下划线开头,并且可以在名称的其余部分包含数字。你知道吗


根据PEP8,建议的样式如下:

https://www.python.org/dev/peps/pep-0008/#package-and-module-names

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.

是的,pythonimport只能接受有效的标识符。你知道吗

选择包括:

  1. 您可以将文件更改为downloadflat_modular.py,然后import downloadflat_modular
  2. 或者可以使用importlib.import_module("downloadflat-modular")。你知道吗

你可以说,这是一种限制。你知道吗

-是减法运算符,因此Python将downloadflat-modular解释为“downloadflat减去modular”,这在import语句之后是不允许的。你知道吗

基本上,您不能(也不应该)在Python中用-符号命名任何东西。你知道吗

相关问题 更多 >

    热门问题