8

I pip the "opencc"

when i shell the code below

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

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

file:version.py

__version__ = '0.1'

when i change

from version import __version__

into

__version__ = '0.1'

opencc,it works

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,

f.BigBro
  • 129
  • 1
  • 2
  • 7

2 Answers2

8

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

Specifically, the version module is part of the opencc package, 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.

Tim
  • 41,901
  • 18
  • 127
  • 145
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thanks. that means the__init__.py import module just as the sys.path,but can not import the module in current path? When I open the __init__.py and run it ,the import can works – f.BigBro Sep 07 '15 at 02:39
  • @f.BigBro: it means that importing in Python 2 starts with looking in the current package, which means that it was very easy to mask globally installed libraries unintentionally. If you pretend that the `opencc` package contents are globally installed modules (by adding it to `sys.path` or by running `__init__.py` directly then yes, it will work, but that doesn't mean the library will work correctly. The whole project has to be ported to Python 3, and unless you have experience doing such a port, I would *not* attempt to do it yourself. – Martijn Pieters Sep 07 '15 at 07:17
  • i forgot to give a comment when i solved the problem. actually.Because I save the file name for "opencc.py",so it happened,changed the name ,it works – f.BigBro Dec 03 '15 at 13:56
0

add package , or coppy it to cp -R Version /usr/local/lib/python3.9 it work for me

nam4lpha
  • 21
  • 1
  • 4