Python 导入错误:没有名为“版本”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32423793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:32:29  来源:igfitidea点击:

ImportError: No module named 'version'

python

提问by f.BigBro

I pip the "opencc"

我点了“opencc”

when i shell the code below

当我对下面的代码进行 shell 时

import opencc

it shows

表明

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import opencc
File "C:\Python34\lib\site-packages\opencc\__init__.py", line 6, in <module>
from version import __version__ 
ImportError: No module named 'version'

but "____init__.py"and"version.py" are in the same directory C:\Python34\lib\site-packages\opencc

但是“____init__.py”和“version.py”在同一个目录 C:\Python34\lib\site-packages\opencc

opencc
    |----__init__.py
    |----version.py

file:version.py

文件:版本.py

__version__ = '0.1'

when i change

当我改变

from version import __version__

into

进入

__version__ = '0.1'

opencc,it works

opencc,它有效

I know it doesn't make a big difference,but i just want to know why the init.py can't import the module version.py in the same directory,

我知道这没有什么大的区别,但我只想知道为什么 init.py 无法在同一目录中导入模块 version.py,

采纳答案by Martijn Pieters

The openccmodule is not compatible with Python 3. It can currently only be used on Python 2.

opencc模块与 Python 3 不兼容。它目前只能在 Python 2 上使用。

Specifically, the versionmodule is part of the openccpackage, but in Python 3 you'd need to use absolute imports, from opencc.version import __version__or from .version import __version__. There will be other issues with the code too.

具体来说,version模块是opencc包的一部分,但在 Python 3 中你需要使用绝对导入,from opencc.version import __version__或者from .version import __version__. 代码也会有其他问题。