BYVoid / OpenCC

Conversion between Traditional and Simplified Chinese
https://opencc.byvoid.com/
Apache License 2.0
8.47k stars 981 forks source link

Cannot import OpenCC using Python on macOS Ventura #759

Closed cverrier closed 1 year ago

cverrier commented 1 year ago

Hi everyone,

Problem Description I get following error when I am running import opencc with Python: OSError: dlopen(libopencc.so.1, 0x0006): tried: 'libopencc.so.1' (no such file).

Steps to Reproduce I am using macOS Ventura 13.1, and Mamba with a virtual environment to use Python (version 3.10.8).

I first installed OpenCC via Homebrew running brew install opencc, and then installed OpenCC with pip in my virtual environment running pip install opencc. The first thing I noticed is that pip is installing OpenCC version 0.2 instead of the latest version (1.1.6 up to now). I also tried to reinstall OpenCC using this file and using brew install opencc.rb, but it led to the same result.

I checked that Homebrew installed correctly OpenCC on my computer: running which opencc, I get the following path: /opt/homebrew/bin/opencc.

I suppose there is a confusion between opencc and libopencc.so.1 libraries, but I am currently unable to fix this problem.

I hope someone could help!

ayaka14732 commented 1 year ago

@mingruimingrui @AntonOfTheWoods Do you know how to fix it?

AntonOfTheWoods commented 1 year ago

@ayaka14732 , @cverrier , for a non-system python project, I don't know what use there could be for brew. Unless I'm mistaken, brew is for system packages. Now maybe the idea is to install into the system packages but I haven't done that outside of a container in maybe a decade... Python projects are best managed by virtual environments unless you want to actually make a system package. And I have never done that so have no idea where to start!

My advice, remove the brew package and start a new virtual env. 1.1.6 should work with Linux and Mac for python 3.7-3.11 and on windows 1.1.5 for python 3.7-3.11.

cverrier commented 1 year ago

@AntonOfTheWoods Thank you for your answer.

First, I thought I needed to install OpenCC using Homebrew, and then do the installation in my Python virtual environment using pip.

My advice, remove the brew package and start a new virtual env. 1.1.6 should work with Linux and Mac for python 3.7-3.11 and on windows 1.1.5 for python 3.7-3.11.

I did exactly what you suggested, using now Python version 3.11.0 in my fresh virtual environment created with mamba. Unfortunately, I still have the same problem: pip actually installs version OpenCC version 0.2, and I am still unable to import the library using Python (same error).

AntonOfTheWoods commented 1 year ago

"mamba is a reimplementation of the conda package manager in C++."

I hadn't heard of mamba before but I now have zero desire to learn about it. conda may have a use but I have only ever had mountains of wasted time and frustration. Maybe ask for some support from their forums?

I personally use asdf (https://asdf-vm.com/guide/getting-started.html) and have never had any issues. Apparently it works just fine on mac. I can help out with debugging, but only if you are using something a little less exotic.

cverrier commented 1 year ago

@AntonOfTheWoods This is my first time hearing about asdf. Since I do not want to install a lot of different things, I simply tried to install OpenCC using virtualenv, i.e. the most simple way to create virtual environments in Python. I also mention that I did not use any Python version installed using conda (nor mamba). The result is exactly the same: pip installs OpenCC 0.2 instead of the latest version, and still unable to import the library (same error as mentioned above).

P.S.: conda is very popular nowadays, especially when doing artificial intelligence / data science. It is super easy to use it, but a lot of people do not understand how does it properly work, that is why they encounter a lot of problems. It basically makes me able to create a standard Python virtual environment, nothing more.

I can help out with debugging, but only if you are using something a little less exotic.

I personally think conda is far less exotic than asdf. At least, virtualenv is even less exotic, and the installation should definitely work using this last.

uituit commented 1 year ago

When I run the command "pip install opencc" on my mac, the below message appeared:

DEPRECATION: opencc is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559

AntonOfTheWoods commented 1 year ago

@uituit , and that is stopping you from installing 1.1.6?

uituit commented 1 year ago

Yes, when I run "pip3 install opencc==1.1.6", I found the following message

pip3 install opencc==1.1.6 ERROR: Could not find a version that satisfies the requirement opencc==1.1.6 (from versions: 0.1, 0.2) ERROR: No matching distribution found for opencc==1.1.6

I started to wonder whether it's because I am using M2 MacBook Air while the wheel file is for x86.

AntonOfTheWoods commented 1 year ago

@uituit , that sounds pretty likely. I have no idea about M2 compatibility but it isn't x86, and there are c++ binaries built and packaged in the wheel. I don't have access to Apple (M1-2) hardware and personally have no interest (or use) in learning about what github might provide (maybe one day I will if Linux support gets any good...). I am not a maintainer of this repo, only an occasional contributor, so all I can do is suggest you help out building and debugging an M2 version!

cverrier commented 1 year ago

@uituit Thank you for the feedback. I also mention that I am running on an Apple M1 Pro Chip.

uituit commented 1 year ago

@AntonOfTheWoods @cverrier Thank you for the feedback, I just installed ver 1.1.6 on my Mac manually.

  1. Download OpenCC as a zip file and extract it
  2. Use pip to install cmake
  3. In Terminal, go to the OpenCC folder and run "python setup.py build_ext bdist_wheel" to create the binary (This should create "lib.macosx-10.9-universal2-cpython-311" in the "build" folder)
  4. Go back to where the OpenCC-master.zip is, and run "pip3 install OpenCC-master.zip" (This should force pip to install by using the setup.py in the zip file)
  5. Go to folder "lib.macosx-10.9-universal2-cpython-311", and copy "opencc_clib.cpython-311-darwin.so" and the "share" folder
  6. Paste them into /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/opencc/clib
  7. Try "import opencc" in IDLE, it should work

Also tested on M2 MacBook Air after installing miniconda, running "pip3 install opencc" installs version 1.1.6.

I am not sure whether the issue is caused by the "release-pypi-macos.sh" script making miniconda a requirement, or it specifies the use of 'x86_64' chips set.

Anyway, the library is working on my end now.

Updated (GMT 9 Jan 2023 - 20:25): Following my earlier investigation, I created a pyproject.toml following the information provided in setup.py and built OpenCC-1.1.6-cp311-cp311-macosx_11_0_arm64.whl

https://packaging.python.org/en/latest/specifications/declaring-project-metadata/

woaying commented 1 year ago

I had the exactly same problem with import opencc after pip install opencc, and I saw @uituit post about the solution. I was about to try it out but got confused about step3 "go to the OpenCC folder". I don't know which "OpenCC" folder he mentioned here.

So I found an easier solution online: https://clay-atlas.com/us/blog/2021/03/31/nlp-python-en-opencc-convert-chinese-traditional-simplified/ pip install opencc-python-reimplemented It'll install opencc-python-reimplemented-0.1.6, but at lease I'm able to convert the characters.

cverrier commented 1 year ago

I tried to reinstall OpenCC from scratch today using the most basic Python setup, and it still does not work. I have looked at the suggestions above, but I think it is way too complex to install such a simple thing.

Does anyone plan to fix that?

uituit commented 1 year ago

I tried to reinstall OpenCC from scratch today using the most basic Python setup, and it still does not work. I have looked at the suggestions above, but I think it is way too complex to install such a simple thing.

Does anyone plan to fix that?

My suggestion above, in short, is to install cmake and compile OpenCC manually after downloading the entire project as a zip file. The problem is the setup.py in the package is also a bit problematic when working with the latest version of pip (probably what you meant by 'the most basic Python setup' is without using condo or virtual environment).

AntonOfTheWoods commented 1 year ago

@cverrier , the project doesn't support Mac silicon (or whatever you call it) yet. I don't have such hardware and currently have no personal interest in finding out how to simulate it. You clearly have such hardware, and appear to be a developer. You could help out. There are other projects that probably do similar things if you don't have the time or inclination to do so.

huang06 commented 1 year ago

That's how I install Python opencc on Apple M1.

My environment:

brew install opencc
> which opencc
/opt/homebrew/bin/opencc
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -U pip setuptools wheel

git clone https://github.com/BYVoid/OpenCC.git
cd OpenCC
python3 setup.py build_ext
python3 -m pip install .
> python3 -c 'import opencc; print(opencc.__version__);'
1.1.6
> python3 -m pip list
Package    Version
---------- -------
OpenCC     1.1.6
pip        22.3.1
setuptools 67.0.0
wheel      0.38.4
cverrier commented 1 year ago

@AntonOfTheWoods I understand. Unfortunately, I do not have time to contribute to the project right now.

@huang06 Thanks a lot for that solution, it works perfectly (even using conda so as to create the virtual environment)!

peteliu66 commented 9 months ago

That's how I install Python opencc on Apple M1.

My environment:

  • Apple M1 13.1
  • Homebrew 3.6.20-146-g4b3d64d
  • Python 3.10.9
brew install opencc
> which opencc
/opt/homebrew/bin/opencc
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -U pip setuptools wheel

git clone https://github.com/BYVoid/OpenCC.git
cd OpenCC
python3 setup.py build_ext
python3 -m pip install .
> python3 -c 'import opencc; print(opencc.__version__);'
1.1.6
> python3 -m pip list
Package    Version
---------- -------
OpenCC     1.1.6
pip        22.3.1
setuptools 67.0.0
wheel      0.38.4

When I run command python3 setup.py build_ext iterm catch an exception: building libopencc into /Users/pete/Projects/python/OpenCC/build/python error: [Errno 2] No such file or directory: 'cmake' What should I do to solve this problem

ayaka14732 commented 8 months ago

@peteliu66 According to the error message, you should install cmake.

krahets commented 7 months ago

@huang06 It works well, thanks! For me, building from source without brew install worked:

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -U pip setuptools wheel

git clone https://github.com/BYVoid/OpenCC.git
cd OpenCC
python3 setup.py build_ext
python3 -m pip install .